首页 文章

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

提问于
浏览
200

我有一个用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(...)

但他们都没有做好 .

14 回答

  • 8

    虽然回答很晚,但想补充说明这一点

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

    其中 item 是要添加子布局的父布局 .

  • 99

    我不确定我是否已按照您的问题进行操作 - 您是否尝试将子视图附加到RelativeLayout?如果是这样,你想要做的事情:

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

    您膨胀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);
    
  • 2

    添加到这一点是有帮助的,即使它是一个旧帖子,如果要将从xml中膨胀的子视图添加到视图组布局中,则需要调用inflate,其中包含它将要进入哪种类型的视图组的线索被添加到 . 喜欢:

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

    inflate方法非常重载,并在文档中描述了这部分用法 . 我有一个问题,从xml中膨胀的单个视图在父进程中没有正确对齐,直到我进行了这种类型的更改 .

  • 1

    更简单的方法是使用

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

    布局通胀

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

    如果您不在活动中,可以使用 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年前但仍值得一提)

  • 18
  • 6

    如果您想多次添加单个视图,则必须使用

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

    然后它将抛出所有准备添加视图的异常 .

  • 2

    AttachToRoot Set to 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);
    

    希望这个解决方案适合您 .

  • 351

    由于我的独特情况,我遇到了这个错误最难的时候,但终于找到了解决方案 .

    我的情况:我正在使用一个单独的视图(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();
    
  • -1

    如果您要将子视图附加到RelativeLayout?你可以通过以下方式做到

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

    使用Kotlin,您可以使用:

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

    为此我使用了下面的代码片段,它对我有用 .

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

相关问题