如何使用布局来扩充一个视图

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)

  • 刚试过这个,我需要:查看child = getLayoutInflater().inflate(R.layout.child,null); (27认同)
  • 这并不完美.应该使用3-param版本的inflater - 这就是为什么:http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ (14认同)
  • 我收到一个错误:02-25 22:21:19.324:ERROR/AndroidRuntime(865):引起:java.lang.IllegalStateException:指定的子节点已经有父节点.您必须首先在孩子的父母上调用removeView(). (3认同)
  • 问题不在于代码.遵循代码相当容易.你在哪里制作xml文件.是主题样式资源的类型?问题我认为我包括的人是我们放置这个xml文件的位置 (3认同)
  • 答案是不正确的,即使它有效.请查看此帖子https://possiblemobile.com/2013/05/layout-inflation-as-intended/ (3认同)

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)

  • 我的主要观点膨胀了.问题是我想从其他XML文件中膨胀部分内容.这个项目RelativeLayout是更大布局的一部分,我想用另一个XML文件的布局填充它. (4认同)

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)


Jua*_*tés 6

如果您不在活动中,则可以使用类中的静态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

试试这个代码:

  1. 如果你只是想膨胀你的布局:

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)

  1. 如果你想在容器(父布局)中膨胀你的布局:

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)


eSp*_*eam 5

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)

希望这个解决方案对你有用。!