安卓:自定义视图基于布局:怎么了?自定义、视图、布局

由网友(隔壁家要糖的小仙女)分享简介:我建立一个Android应用程序,我有点自定义视图挣扎。I am building a Android app and I am a bit struggling with custom Views.我想有一个可重复使用的视图,它由几个标准布局元素。比方说,一些按钮的RelativeLayout的在里面。I wo...

我建立一个Android应用程序,我有点自定义视图挣扎。

I am building a Android app and I am a bit struggling with custom Views.

我想有一个可重复使用的视图,它由几个标准布局元素。比方说,一些按钮的RelativeLayout的在里面。

I would like to have a reusable View that consist of a few standard layout elements. Let's say a relativelayout with some buttons in it.

我应该如何着手。我应该创建扩展RelativeLayout的和programmaticly添加这些按键的自定义视图类?我认为这是一个有点矫枉过正?

How should I proceed. Should I create a custom view class that extends RelativeLayout and programmaticly add those buttons? I would think that's a bit overkill?

什么是做正确的的Andr​​oid的方式吗?

What's the way to do it properly in Android?

推荐答案

下面是关于创建自定义的聚合视图的一种方式有些粗糙步骤:

Here are some rough steps regarding one way to create a custom aggregate view:

延长RelativeLayout的 提供新类的构造函数接受语境的AttributeSet,确保首先调用父类。难道没有在这一点上添加任何东西。等到下一个步骤。 重写onFinishInflate方法,在这里你可以通过Java code添加的内容或充气XML资源 添加任何事件处理程序,等等

选择创建一个资源文件,如果要设置你的widget需要的属性。 extend RelativeLayout Provide a constructor in your new class that accepts Context and AttributeSet, making sure to call the superclass first. Do no add anything at this point. Wait until the next step. override the onFinishInflate method, where you can add your contents through Java code or inflating an XML resource Add any event handlers, etc

Optionally create a resources file if your widget will require attributes to be set.

                  

阅读全文

相关推荐

最新文章