我在XML定义的布局。它包含也:

<RelativeLayout
    android:id="@+id/item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

我想膨胀此RelativeView与其他XML布局文件。我可以用根据情况不同的布局。我应该怎么办呢?我试图的不同变型

RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)

但是他们没有正常工作。

有帮助吗?

解决方案

我不知道我按照您的question-你试图连接一个子视图到RelativeLayout的?如果是这样,你想做的事线的东西:

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);

其他提示

您膨胀XML资源。请参阅 LayoutInflater DOC

如果您的布局中的 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);

虽然晚答案, 但想补充一点,一个办法让这个

LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.mylayout, item );

其中item是要增加一个子布局的父布局。

这是有帮助添加到这一点,即使它是一个老帖子,如果正从XML充气子视图将被添加到一个ViewGroup中的布局,你需要调用inflate什么类型的ViewGroup的线索它会被添加到。像:

View child = getLayoutInflater().inflate(R.layout.child, item, false);

在充气方法是相当重载,并描述在文档的使用的这部分。我有其中单个视图从XML膨胀的问题在父没有正确对准,直到我在此类型的更改。

更简单的方法是使用

View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item

布局充气

View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);

如果您在活动是不是你可以使用静态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);

(我知道这是近4年前,但仍然值得一提的)

如果要添加的单个视图中的多个时间,那么你必须使用

   layoutInflaterForButton = getActivity().getLayoutInflater();

 for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
        FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
        btnContainer.addView(btnView);
    }

如果你不喜欢

   layoutInflaterForButton = getActivity().getLayoutInflater();
    FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);

for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
            btnContainer.addView(btnView);
        }

然后它会抛出所有准备加入视图的例外。

<强> AttachToRoot设为True

试想我们在与它的布局宽度和布局高度设置为一个match_parent XML布局文件中指定的按钮。

<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>

在这个按钮点击事件,我们可以设置在该活动上下面的代码膨胀布局。

LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);

希望这解决方案适用于你!

我有困难的时候与这个错误,因为我的特殊情况,但最后还是找到了解决办法。

我的情况:我使用的持有WebView,然后在AlertDialog,当我在我的主要活动视图中单击一个按钮打开一个单独的视图(XML)。但不知何故或其他的WebView属于主要活动视图(可能是因为我拉从这里的资源),这样对我分配到我的AlertDialog之前(如视图),我必须让我的WebView的父母,把它成ViewGroup,然后删除对ViewGroup各方面的意见。这个工作,我的错误就走开了。

// set up Alert Dialog box
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// inflate other xml where WebView is
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.your_webview_layout, null);
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id);

// more code...

....后来我加载之后我WebView ....

// first, remove the parent of WebView from it's old parent so can be assigned a new one.
ViewGroup vg = (ViewGroup) webView.getParent();
vg.removeAllViews();

// put WebView in Dialog box
alert.setView(webView);
alert.show();

如果你是你试图附加一个子视图到RelativeLayout的?可以通过以下做

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);

试试这个代码:

  1. 如果你只是想抬高你的布局:
  2. View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
    RelativeLayout item = view.findViewById(R.id.item);   

  • 如果您想在容器膨胀布局(父布局):
  • 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.

    使用科特林,可以使用:

    val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
    
    [your_main_layout].apply {
        //..
        addView(content)
    }
    

    我下面的代码段用于此,它适合我。

    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
    View child = getLayoutInflater().inflate(R.layout.child, null);
    linearLayout.addView(child);
    

    没有需要任何复杂性,它只是简单

     setContentView(R.layout.your_layout);
    
    许可以下: CC-BY-SA归因
    不隶属于 StackOverflow
    scroll top