自定义View与merge节点
2016-02-23 19:15
330 查看
之前写自定义ViewGroup的时候,一般都像下面:
merge_test.xml如下:
这样实际上是有性能问题的,会产生一个RelativeLayout(TestView本身)套嵌另一个RelativeLayout(xml)的现象。
还有另外一个问题,如果使用TestView的measure、layout、draw等方法(不是onXXX),实际上只是直接作用在其子View(xml的RelativeLayout)上,可能有潜在问题。
合理的方法是将xml里的RelativeLayout改为merge。merge不会检查根View的attr,比如android:layout_below等等,所有东西完全可用。但是根View(TestView)本身的一些参数,要自己调用函数设置(比如,LinearLayout的Orientation)。
这样可以减少一层ViewGroup,还可以减少想当然的调用问题。
public class TestView extends RelativeLayout { public TestView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { LayoutInflater.from(context).inflate(R.layout.merge_test, this, true); } }
merge_test.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/b1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/b2" android:text="aaa" /> <Button android:id="@+id/b2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="bbb" /> </RelativeLayout>
这样实际上是有性能问题的,会产生一个RelativeLayout(TestView本身)套嵌另一个RelativeLayout(xml)的现象。
还有另外一个问题,如果使用TestView的measure、layout、draw等方法(不是onXXX),实际上只是直接作用在其子View(xml的RelativeLayout)上,可能有潜在问题。
合理的方法是将xml里的RelativeLayout改为merge。merge不会检查根View的attr,比如android:layout_below等等,所有东西完全可用。但是根View(TestView)本身的一些参数,要自己调用函数设置(比如,LinearLayout的Orientation)。
这样可以减少一层ViewGroup,还可以减少想当然的调用问题。
相关文章推荐
- Ubuntu中安装oracle java和Eclipse
- TLD学习之剥离Randrom Fern
- LeetCode 30 Substring with Concatenation of All Words--In Java
- JVM性能监控与故障处理工具
- Apple Pay--iOS开发
- Centos 挂载NTFS格式的USB硬盘
- ZOJ-1586QS Network(prim)
- 使用RNN解决NLP中序列标注问题的通用优化思路
- 0016-jQuery选择器与CSS选择器
- js中数组的用法
- vs2010断点调试技巧分享
- Linux内核分析:实验一
- Android MotionEvent分发机制
- spark源码学习(七);task任务的提交分析
- 网络编程2---select poll epoll
- [转]nutch1.2断电或者断网后继续爬取的方式
- [转] 用JAVA 实现“生产者-消费者”问题
- nutch1.2爬虫在eclipse下运行遇到的问题
- js去除数组的重复元素和去除数组中相邻的重复元素
- [置顶] 换ojdbc14.jar驱动后clob字段后台报异常的解决