【问题标题】:How to inflate one view with a layout如何使用布局膨胀一个视图
【发布时间】:2010-02-25 16:47:51
【问题描述】:

我有一个用 XML 定义的布局。它还包含:

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

我想用其他 XML 布局文件扩充这个 RelativeView。我可能会根据情况使用不同的布局。我该怎么做?我正在尝试不同的变体

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

但没有一个能正常工作。

【问题讨论】:

    标签: android android-layout layout-inflater android-inflate


    【解决方案1】:

    我不确定我是否遵循了您的问题 - 您是否尝试将子视图附加到 RelativeLayout?如果是这样,您想按照以下方式做一些事情:

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

    【讨论】:

    • 我得到一个错误:02-25 22:21:19.324: ERROR/AndroidRuntime(865): Caused by: java.lang.IllegalStateException: The specified child has a parent.您必须先在孩子的父母上调用 removeView()。
    • 刚刚试了一下,我需要:View child = getLayoutInflater().inflate(R.layout.child,null);
    • 问题不在于代码。遵循代码相当容易。你在哪里制作xml文件。类型是主题样式资源吗?我认为包括我在内的人的问题是我们将这个 xml 文件放在哪里
    • 这并不完美。应该使用 3 参数版本的充气机 - 原因如下:doubleencore.com/2013/05/layout-inflation-as-intended
    • 答案是不正确的,即使它有效。请查看此帖子possiblemobile.com/2013/05/layout-inflation-as-intended
    【解决方案2】:

    您扩充了 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);
    

    【讨论】:

    • 我的主要观点被夸大了。问题是我想从其他 XML 文件中扩充它的一部分。此项目 RelativeLayout 是更大布局的一部分,我想用另一个 XML 文件中的布局填充它。
    • 嗯,对不起,我不知道 :( 。您是否在主视图中使用类似 的内容来包含其他布局?
    • 不,这是简单的布局。我想我只会以编程方式添加我的元素 - 没有 XML。感谢您的帮助!
    【解决方案3】:

    虽然回答迟了, 但想添加一种方法来获得这个

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

    其中item 是您要在其中添加子布局的父布局。

    【讨论】:

    • (甚至更晚)我很难实现你的答案。我尝试了您上面提到的代码,但将 'item' 替换为:'R.layout.activity_layout' 我得到一个“没有适用于 '(int,int)' 的方法”我能麻烦你吗?
    • @Kyle - 甚至更晚。您不能使用R 资源作为对对象的引用,它们只是ints。您必须使用findViewByIdR 资源传递给它,然后将其类型转换为您想要的对象。然后你可以在像inflate这样的函数调用中使用它。 (即ViewGroup item = (ViewGroup) findViewById(R.layout.activity_layout);...然后你可以像上面一样使用item。)
    • 我想在上面的示例中我应该使用RelativeLayout 而不是ViewGroup,因为您无法实例化基类ViewGroup。 5分钟的编辑规则让我着迷。大声笑
    • 是否可以隐藏父视图并仅显示子视图
    • 谢谢,这个方法帮助解决了我在上一个示例中无法使用 getContext() 的问题,因为我使用的是片段。
    【解决方案4】:

    添加到这里是有帮助的,即使它是一个旧帖子,如果从 xml 膨胀的子视图要添加到视图组布局,你需要调用 inflate 并知道什么类型的视图组它将被添加到。喜欢:

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

    inflate 方法非常重载,并在文档中描述了这部分用法。在我进行此类更改之前,我遇到了一个问题,即从 xml 膨胀的单个视图在父级中没有正确对齐。

    【讨论】:

    • 最后,谢谢 :) 这允许向单个父级添加多个视图,但无需担心从 XML 读取的 LayoutParams。干得好!
    【解决方案5】:

    更简单的方法是使用

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

    【讨论】:

    • 使用View.inflate() 是最好的方法,没有不必要的LayoutInflatergetSystemService(Context.LAYOUT_INFLATER_SERVICE) 或任何令人讨厌的东西! :D
    【解决方案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);   
    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.

    【讨论】:

      【解决方案7】:

      布局膨胀

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

      【讨论】:

        【解决方案8】:

        如果您不在活动中,您可以使用LayoutInflater 类中的静态from() 方法来获取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 年前,但仍然值得一提)

        【讨论】:

          【解决方案9】:

          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>
          

          在此按钮上单击事件我们可以设置以下代码以在此活动上膨胀布局。

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

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

          【讨论】:

            【解决方案10】:

            如果你想多次添加一个视图,那么你必须使用

               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);
                    }
            

            然后它将抛出所有已添加视图的异常。

            【讨论】:

              【解决方案11】:

              如果您想将子视图附加到 RelativeLayout?您可以按照以下方式进行操作

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

              【讨论】:

                【解决方案12】:

                由于我的特殊情况,我遇到这个错误最艰难,但最终找到了解决方案。

                我的情况:我正在使用一个单独的视图 (XML),其中包含一个 WebView,然后当我单击主活动视图中的一个按钮时以 AlertDialog 打开。但不知何故,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();
                

                【讨论】:

                  【解决方案13】:

                  使用 Kotlin,您可以使用:

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

                  【讨论】:

                    【解决方案14】:

                    我为此使用了低于 sn-p 的代码,它对我有用。

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

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-09-11
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多