[英]Android: custom view from inflated layout
I am creating my own layout based on RelativeLayout
as a class in code 我正在基于
RelativeLayout
作为代码中的类创建自己的布局
I have basics of the layout defined in XML R.layout.menu_layout
(style, drawable for background, margin, height) 我有在XML
R.layout.menu_layout
定义的布局的基础(样式,可绘制背景,边距,高度)
If I would not need a class then I would call inflater to do this: 如果我不需要上课,那么我会打电话给充气机做到这一点:
RelativeLayout menuLayout = (RelativeLayout)inflater.inflate(R.layout.menu_layout, root);
But I would like to be calling my own class instead 但是我想打电话给我自己的课
MenuLayout menuLayout = new MenuLayout(myparams);
Since I need to create a class I need to somehow inherit the R.layout.menu_layout
in constructor, how can I do that? 由于我需要创建一个类,因此需要以某种方式继承构造函数中的
R.layout.menu_layout
,该怎么办? I guess there is no this.setLayout(res);
我猜没有
this.setLayout(res);
or this.setResource(res);
或
this.setResource(res);
in View. 在视图中。 Maybe I can use the other two parameters in View constructor but I did not find any tutorial how to do that either.
也许我可以在View构造函数中使用其他两个参数,但是我都没有找到任何教程来做到这一点。
public class MenuLayout extends RelativeLayout {
public MenuLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public MenuLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MenuLayout(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.menu_layout, null);
addView(view);
}
}
now you can use 现在您可以使用
MenuLayout menuLayout = new MenuLayout(myparams);
you can change constructors for params i think 您可以更改参数的构造函数,我认为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.