我对Android很陌生 ,我想实现一个目标,但目前我找不到合适的解决方案。
我正在开发Android,我需要一个Activity,它显示一个带有圆角的2D矩形的二维网格 。 网格的大小在运行时确定并从父活动转发。 这意味着我还必须在运行时创建矩形 。 由于矩形也需要自定义行为,我尝试了以下方法:
我确实编写了自己的类,并从View继承。 在onDraw()方法中,我定义了矩形的外观,即使在开始时我想在ShapeDrawable XML文件中定义外观并将其加载到我的自定义矩形的构造中。 这不太好用。 现在我卡住了,因为当我尝试将我的视图添加到布局,并将其固定到显示网格的活动时,我会收到错误。 当我使用setContentView(rectangle)直接设置其中一个矩形时,它会显示在屏幕中间的某个位置。
所以我的问题是:
它是通过继承View来创建这些矩形的正确方法吗? (请记住,我必须修改用户点击矩形时绘制的方式,我想稍后点击一个矩形时打开一个菜单)
显示这些矩形的最佳方法是什么? 当我想要以网格状的方式显示它们时,我应该使用哪种布局? 用户以后也应该能够删除一些矩形。 因此必须有可能在网格中有间隙 。
由于使用此代码将矩形添加到布局无效,我该怎么做(我使用了LinearLayout)?
ViewGroup layout = (ViewGroup) findViewById(R.id.conf_new_solar_plant_layout); SolarPanelView spView = new SolarPanelView(this); spView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); layout.addView(spView)我收到的错误如下:
11-06 11:41:49.862: E/AndroidRuntime(2203): FATAL EXCEPTION: main 11-06 11:41:49.862: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartexergy.app/com.smartexergy.app.ConfNewSolarPlantActivity}: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.access$600(ActivityThread.java:139) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Looper.loop(Looper.java:154) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.main(ActivityThread.java:4944) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invoke(Method.java:511) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-06 11:41:49.862: E/AndroidRuntime(2203): at dalvik.system.NativeStart.main(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): Caused by: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.smartexergy.app.ConfNewSolarPlantActivity.onCreate(ConfNewSolarPlantActivity.java:34) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Activity.performCreate(Activity.java:4531) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)谢谢!
编辑:修复了错误,我忘了将布局设置为contentView。 其余问题仍然存在。
I'm quite new to Android and there is a goal I want to achieve but I can't find a proper solution at the moment.
I'm developing in Android and I need an Activity that displays a two dimensional grid of 2D rectangles with round corners. The size of the grid gets determined at runtime and forwarded from the parent Activity. That means I also have to create the rectangles at runtime. As the rectangles also need a custom behaviour I tried the following:
I did write my own class and did inherit from View. In the onDraw() method I defined how the rectangle should look like, even though at the beginning I wanted to define the look in a ShapeDrawable XML file and load it in the construction of my custom rectangle. That didn't work well. Now I'm stuck, because when I try to add my view to the layout assined to the activity to display the grid, I get errors. When I set one of those rectangles directly with setContentView(rectangle) it gets displayed somewhere in the middle of the screen.
So my questions are:
Is it the right way to create these rectangles by inheriting from View? (keep in mind that I have to modify the way the are drawn when the user does click on a rectangle and I want to open a menu later when a rectangle gets clicked)
What's the best way to display these rectangles? Which kind of layout should I use when I wanted display them in a grid-like fashion? The user should later also be able to delete some rectangles. So it must be possible to have gaps in the grid.
As it didn't work to add the rectangles to a layout with this code, how should I do it (I used a LinearLayout)?
ViewGroup layout = (ViewGroup) findViewById(R.id.conf_new_solar_plant_layout); SolarPanelView spView = new SolarPanelView(this); spView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); layout.addView(spView)The error I receive is the following:
11-06 11:41:49.862: E/AndroidRuntime(2203): FATAL EXCEPTION: main 11-06 11:41:49.862: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartexergy.app/com.smartexergy.app.ConfNewSolarPlantActivity}: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.access$600(ActivityThread.java:139) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Looper.loop(Looper.java:154) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.main(ActivityThread.java:4944) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invoke(Method.java:511) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-06 11:41:49.862: E/AndroidRuntime(2203): at dalvik.system.NativeStart.main(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): Caused by: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.smartexergy.app.ConfNewSolarPlantActivity.onCreate(ConfNewSolarPlantActivity.java:34) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Activity.performCreate(Activity.java:4531) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)Thanks!
EDIT: Got the error fixed, I forgot to set the layout as the contentView. The rest of the questions still remain.
最满意答案
我继续做一些研究,在我的特殊情况下,我确实发现它是最简单的,也是一种编写我自己的扩展ImageView的类的正确方法。 在ImageView中,我确实覆盖了onDraw()方法以获得我的自定义形状和设计。
要在网格中显示此视图,我使用了GridLayout并以编程方式添加了我的自定义视图。 在GridLayout中,它可以为每个项目提供ax和y坐标。 关于差距我目前还不太确定,但如果不可能,我会添加“空间”对象以获得空隙。
点击事件很容易在ImageView中捕获,但我认为它也应该在GridLayout中,所以我可以处理我的矩形的移动和删除。
I continued doing some research and in my particular case I did find out that its the easiest and also a proper way to write my own iew class that extends ImageView. In ImageView I did override the onDraw() method to get my custom shape and design.
To display this views in a grid I used a GridLayout and added my custom views programatically. In the GridLayout its possible to give each item a x and y coordinate. About the gaps I'm not quite sure at the moment, but if its not possible I will add "space" objects to have empty gaps.
Click events are easy to catch in a ImageView, but I think it should be also possible within the GridLayout, so I can handle the movement and deletion of my rectangles.
Android中自定义矩形视图的体系结构(Architecture for custom rectangular views in Android)我对Android很陌生 ,我想实现一个目标,但目前我找不到合适的解决方案。
我正在开发Android,我需要一个Activity,它显示一个带有圆角的2D矩形的二维网格 。 网格的大小在运行时确定并从父活动转发。 这意味着我还必须在运行时创建矩形 。 由于矩形也需要自定义行为,我尝试了以下方法:
我确实编写了自己的类,并从View继承。 在onDraw()方法中,我定义了矩形的外观,即使在开始时我想在ShapeDrawable XML文件中定义外观并将其加载到我的自定义矩形的构造中。 这不太好用。 现在我卡住了,因为当我尝试将我的视图添加到布局,并将其固定到显示网格的活动时,我会收到错误。 当我使用setContentView(rectangle)直接设置其中一个矩形时,它会显示在屏幕中间的某个位置。
所以我的问题是:
它是通过继承View来创建这些矩形的正确方法吗? (请记住,我必须修改用户点击矩形时绘制的方式,我想稍后点击一个矩形时打开一个菜单)
显示这些矩形的最佳方法是什么? 当我想要以网格状的方式显示它们时,我应该使用哪种布局? 用户以后也应该能够删除一些矩形。 因此必须有可能在网格中有间隙 。
由于使用此代码将矩形添加到布局无效,我该怎么做(我使用了LinearLayout)?
ViewGroup layout = (ViewGroup) findViewById(R.id.conf_new_solar_plant_layout); SolarPanelView spView = new SolarPanelView(this); spView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); layout.addView(spView)我收到的错误如下:
11-06 11:41:49.862: E/AndroidRuntime(2203): FATAL EXCEPTION: main 11-06 11:41:49.862: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartexergy.app/com.smartexergy.app.ConfNewSolarPlantActivity}: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.access$600(ActivityThread.java:139) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Looper.loop(Looper.java:154) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.main(ActivityThread.java:4944) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invoke(Method.java:511) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-06 11:41:49.862: E/AndroidRuntime(2203): at dalvik.system.NativeStart.main(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): Caused by: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.smartexergy.app.ConfNewSolarPlantActivity.onCreate(ConfNewSolarPlantActivity.java:34) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Activity.performCreate(Activity.java:4531) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)谢谢!
编辑:修复了错误,我忘了将布局设置为contentView。 其余问题仍然存在。
I'm quite new to Android and there is a goal I want to achieve but I can't find a proper solution at the moment.
I'm developing in Android and I need an Activity that displays a two dimensional grid of 2D rectangles with round corners. The size of the grid gets determined at runtime and forwarded from the parent Activity. That means I also have to create the rectangles at runtime. As the rectangles also need a custom behaviour I tried the following:
I did write my own class and did inherit from View. In the onDraw() method I defined how the rectangle should look like, even though at the beginning I wanted to define the look in a ShapeDrawable XML file and load it in the construction of my custom rectangle. That didn't work well. Now I'm stuck, because when I try to add my view to the layout assined to the activity to display the grid, I get errors. When I set one of those rectangles directly with setContentView(rectangle) it gets displayed somewhere in the middle of the screen.
So my questions are:
Is it the right way to create these rectangles by inheriting from View? (keep in mind that I have to modify the way the are drawn when the user does click on a rectangle and I want to open a menu later when a rectangle gets clicked)
What's the best way to display these rectangles? Which kind of layout should I use when I wanted display them in a grid-like fashion? The user should later also be able to delete some rectangles. So it must be possible to have gaps in the grid.
As it didn't work to add the rectangles to a layout with this code, how should I do it (I used a LinearLayout)?
ViewGroup layout = (ViewGroup) findViewById(R.id.conf_new_solar_plant_layout); SolarPanelView spView = new SolarPanelView(this); spView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); layout.addView(spView)The error I receive is the following:
11-06 11:41:49.862: E/AndroidRuntime(2203): FATAL EXCEPTION: main 11-06 11:41:49.862: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartexergy.app/com.smartexergy.app.ConfNewSolarPlantActivity}: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.access$600(ActivityThread.java:139) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.os.Looper.loop(Looper.java:154) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.main(ActivityThread.java:4944) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): at java.lang.reflect.Method.invoke(Method.java:511) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-06 11:41:49.862: E/AndroidRuntime(2203): at dalvik.system.NativeStart.main(Native Method) 11-06 11:41:49.862: E/AndroidRuntime(2203): Caused by: java.lang.NullPointerException 11-06 11:41:49.862: E/AndroidRuntime(2203): at com.smartexergy.app.ConfNewSolarPlantActivity.onCreate(ConfNewSolarPlantActivity.java:34) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Activity.performCreate(Activity.java:4531) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 11-06 11:41:49.862: E/AndroidRuntime(2203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)Thanks!
EDIT: Got the error fixed, I forgot to set the layout as the contentView. The rest of the questions still remain.
最满意答案
我继续做一些研究,在我的特殊情况下,我确实发现它是最简单的,也是一种编写我自己的扩展ImageView的类的正确方法。 在ImageView中,我确实覆盖了onDraw()方法以获得我的自定义形状和设计。
要在网格中显示此视图,我使用了GridLayout并以编程方式添加了我的自定义视图。 在GridLayout中,它可以为每个项目提供ax和y坐标。 关于差距我目前还不太确定,但如果不可能,我会添加“空间”对象以获得空隙。
点击事件很容易在ImageView中捕获,但我认为它也应该在GridLayout中,所以我可以处理我的矩形的移动和删除。
I continued doing some research and in my particular case I did find out that its the easiest and also a proper way to write my own iew class that extends ImageView. In ImageView I did override the onDraw() method to get my custom shape and design.
To display this views in a grid I used a GridLayout and added my custom views programatically. In the GridLayout its possible to give each item a x and y coordinate. About the gaps I'm not quite sure at the moment, but if its not possible I will add "space" objects to have empty gaps.
Click events are easy to catch in a ImageView, but I think it should be also possible within the GridLayout, so I can handle the movement and deletion of my rectangles.
发布评论