如何在安卓中将布局插入到另一个布局中?

12

我有两个布局,分别是first和second。我想要将second插入到first中。 我希望将second布局插入到具有id为@+id/layout的布局中。 当我点击“获取布局”按钮时,它应该在按钮底部显示第二个布局。

第一个布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
    android:id="@+id/btn_get_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Layout" />

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

</LinearLayout>

第二个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_base"
android:orientation="vertical" >


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img_cover"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/card_beauty" />

    <ImageView
        android:id="@+id/img_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/sample_0" />
</RelativeLayout>

</LinearLayout>
5个回答

34

如果我理解正确,您应该在第一个布局中使用以下代码:

<include 
     layout="@layout/second_layout"
     android:id="@+id/includedLayout"
     android:visibility="gone"
     android:layout_below="@id/buttonId" />

然后在您的按钮操作中,您只需使用以下代码:

((RelativeLayout)findViewById(R.id.includedLayout)).setVisibility(View.VISIBLE);

12
    LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
    getLayoutInflater().inflate(R.layout.second_layout, placeHolder);

1
如果我需要动态插入多个布局(布局数量事先未知),我该如何做? - LiangWang

11

使用include

这是一个更为详细的例子。使用include将蓝色正方形布局插入到主要布局中。

enter image description here

activity_main.xml

这是主布局。使用include引用自定义布局。你也可以在这里重写任何属性。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- Here is the inserted layout -->
    <include layout="@layout/my_layout"/>

</RelativeLayout>

my_layout.xml

这是将插入到主布局中的自定义布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@color/colorPrimary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:text="My Layout"/>

</RelativeLayout>

1

您应该使用此代码片段将布局添加到另一个布局中。

    parentLayout=(LinearLayout)findViewById(R.id.parentLayout);
    inflateView=View.inflate(this,R.layout.view_video,parentLayout);

这里的parentLayout是根视图,R.layout.view_video是需要插入的布局。


0
使用LayoutInflator将外部布局填充到现有布局中。请查看Android Dev网站上的LayoutInflater,了解更多相关信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接