Mic*_*mel 213 android android-layout android-inflate layout-inflater
我有一个用XML定义的布局.它还包含:
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
我想用其他XML布局文件来扩充这个RelativeView.我可能会根据情况使用不同的布局.我该怎么办?我正在尝试不同的变化
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
Run Code Online (Sandbox Code Playgroud)
但他们都没有做好.
Gra*_*can 378
我不确定我是否已按照您的问题进行操作 - 您是否尝试将子视图附加到RelativeLayout?如果是这样,你想要做的事情:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Run Code Online (Sandbox Code Playgroud)
cch*_*son 102
您膨胀XML资源.请参阅LayoutInflater文档.
如果您的布局位于mylayout.xml中,您可以执行以下操作:
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);
Run Code Online (Sandbox Code Playgroud)
Sha*_*aaz 26
虽然回答很晚,但想补充说明这一点
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
Run Code Online (Sandbox Code Playgroud)
这里item是你想添加一个子布局的父布局.
Max*_*mus 18
添加到这一点是有帮助的,即使它是一个旧帖子,如果要将从xml中膨胀的子视图添加到视图组布局中,则需要调用inflate,其中包含它将要进入哪种类型的视图组的线索被添加到.喜欢:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
Run Code Online (Sandbox Code Playgroud)
inflate方法非常重载,并在文档中描述了这部分用法.我有一个问题,从xml中膨胀的单个视图在父进程中没有正确对齐,直到我进行了这种类型的更改.
Ash*_*wat 13
更简单的方法是使用
View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
Run Code Online (Sandbox Code Playgroud)
小智 8
布局通胀
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);
Run Code Online (Sandbox Code Playgroud)
如果您不在活动中,则可以使用类中的静态from()方法LayoutInflater来获取LayoutInflater,或者也可以从上下文方法请求服务getSystemService():
LayoutInflater i;
Context x; //Assuming here that x is a valid context, not null
i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);
Run Code Online (Sandbox Code Playgroud)
(我知道它差不多4年前但仍值得一提)
小智 6
试试这个代码:
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item); Run Code Online (Sandbox Code Playgroud)
LinearLayout parent = findViewById(R.id.container); //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false);
RelativeLayout item = view.findViewById(R.id.item); //initialize layout & By this you can also perform any event.
parent.addView(view); //adding your inflated layout in parent layout.Run Code Online (Sandbox Code Playgroud)
AttachToRoot 设置为 True
想想我们在 XML 布局文件中指定了一个按钮,其布局宽度和布局高度设置为 match_parent。
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_button">
</Button>
Run Code Online (Sandbox Code Playgroud)
在此按钮上单击事件,我们可以设置以下代码来扩展此活动的布局。
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);
Run Code Online (Sandbox Code Playgroud)
希望这个解决方案对你有用。!