我通常会创建一个自定义视图,例如
public class MyView extends LinearLayout{
public MyView(Context context){
super(context);
//Inflating my custom layout
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.view_layout, this);
}
}
Run Code Online (Sandbox Code Playgroud)
上面布局的问题是,它将在其中创建一个新的LinearLayout并膨胀R.layout.view_layout,添加一个新的视图层次结构,这是不受欢迎的。
R.layout.view_layout包含RelativeLayout许多子元素,我无法通过扩展对其进行编程添加RelativeLayout(因为定位很困难)。
不必要的层次结构会使我的应用程序变慢。
custom views/view group没有额外的层次结构级别的最佳创建方法是什么。
根据CommonsWare的解决方案:
XML布局:
<merge>
<TextView ...
.../>
More items
</merge>
Run Code Online (Sandbox Code Playgroud)
由于XML布局具有RelativeLayout父视图,因此我将RelativeLayout在代码中扩展而不是LinearLayout
public class MyView extends RelativeLayout{
public MyView(Context context){
super(context);
//Inflating my custom layout
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.view_layout, this);
}
}
Run Code Online (Sandbox Code Playgroud)
res/layout/view_layout.xml以元素<merge>而不是开始LinearLayout。
| 归档时间: |
|
| 查看次数: |
903 次 |
| 最近记录: |