phe*_*cks 6 java xml layout android
我一直在尝试在Android 1.5中创建一个复合控件(如此处所述),但是没有找到任何关于如何使用XML文件指定布局的好例子.我可以创建一个Activity,然后在构造函数中使用以下代码加载xml文件:
setContentView(R.layout.main);
Run Code Online (Sandbox Code Playgroud)
但是,我想在LinearLayout的子类中执行此操作 - 因此我可以在其他XML布局中使用此复合组件.有点像:
public class CustomView extends LinearLayout
{
public CustomView(Context context) {
super(context);
setupView();
}
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
setupView();
}
public void setupView()
{
setContentView(R.layout.custom); // Not possible
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
Bra*_*rke 14
您必须为自定义视图"膨胀"布局:
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.custom, this, true);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4081 次 |
最近记录: |