安卓:自定义视图基于布局:怎么了? [英] Android: Custom view based on layout: how?

查看:93
本文介绍了安卓:自定义视图基于布局:怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个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:

  1. 延长RelativeLayout的
  2. 提供新类的构造函数接受语境的AttributeSet,确保首先调用父类。难道没有在这一点上添加任何东西。等到下一个步骤。
  3. 重写<一href="http://developer.android.com/intl/fr/reference/android/view/View.html#onFinishInflate%28%29">onFinishInflate方法,在这里你可以通过Java code添加的内容或充气XML资源
  4. 添加任何事件处理程序,等等
  5. 选择创建一个资源文件,如果要设置你的widget需要的属性。

  1. extend RelativeLayout
  2. 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.
  3. override the onFinishInflate method, where you can add your contents through Java code or inflating an XML resource
  4. Add any event handlers, etc
  5. Optionally create a resources file if your widget will require attributes to be set.

                  

这篇关于安卓:自定义视图基于布局:怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆