![](/img/trans.png)
[英]Android/Java: how to define a specific class usable in an xml layout file?
[英]Android Layout XML file with class
我创建了一个扩展RelativeLayout的类。 我还创建了一个xml布局文件。
当我将Activity与它们的xml文件( setContentView(R.layout.main);
)连接时,如何在它们之间“连接”
谢谢!
我的意思是您可以使用LayoutInflater在相对布局中加载xml,但是不确定您应该做什么。 我不确定您要做什么,但是您可以通过这样做将布局包装在另一个视图中。 当您可以使用相同的策略将xml直接加载到视图中时。 因此,您失去了一种复杂性。
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view,null);
addView(view);
您可以在构造函数中使用LayoutInflater来使布局膨胀。
public class RelSub extends RelativeLayout {
public RelSub(Context context) {
super(context);
// TODO Auto-generated constructor stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.main, this);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.