[英]How to make a .xml layout file the layout of a custom View?
我需要创建类似于Web选项卡的窗口小部件,因此我创建了带有线性laoyut和按钮的简单tabs.xml,但是我需要在该视图中插入其他逻辑,因此在Activity中仅包含tabs.xml是不够的(我正在尝试以避免在活动中添加推入所有代码,因为它不属于活动)。 如何在我通过扩展LinearLayout类创建的视图中使用infalte tabs.xml?
您需要膨胀所创建的.xml布局。
像这样做:
public class CustomLinearLayout extends LinearLayout {
// you need this constructor when you use the customview from code
public CustomLinearLayout (Context context) {
super(context);
addView(LayoutInflater.from(context).inflate(R.layout.yourlayout, null));
}
// you need this constructor when the customview is used from .xml
public CustomLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
addView(LayoutInflater.from(context).inflate(R.layout.yourlayout, null));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.