如何在android中插入布局到布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12071829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:39:04  来源:igfitidea点击:

how to insert layout to layout in android?

androidandroid-layout

提问by kongkea

I've got two layouts first and second, I want to insert second to first. I want to insert second layout into layout that has id @+id/layout when I press button Get Layout, it show the the second layout on the bottom of button

我有两个布局,第一个和第二个,我想插入第二个到第一个。当我按下按钮获取布局时,我想将第二个布局插入具有 id @+id/layout 的布局中,它在按钮底部显示第二个布局

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

second layout

第二个布局

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

回答by ColdFire

if I understood you right you should use the following code within your first layout

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

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

and then in your button's action you just use

然后在您的按钮操作中,您只需使用

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

回答by Sameer

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

回答by Suragch

Use include

include

Here is a somewhat fuller example. The square blue layout is inserted into the main layout using include.

这是一个更完整的例子。使用 将方形蓝色布局插入到主布局中include

enter image description here

在此处输入图片说明

activity_main.xml

活动_main.xml

This is the main layout. The custom layout is referenced using include. You can override any of the attributes here, too.

这是主要布局。使用 引用自定义布局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

my_layout.xml

This is the custom layout that will be inserted into the main layout.

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

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

回答by Febin Mathew

You Should use this code snippet to add a Layout inside another one.

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

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

Here the parentLayoutis the root View and R.layout.view_videois the layout that you need to insert.

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

回答by Chitranshu Asthana

Use LayoutInflator to inflate an external layout into existing layout. Checkout LayoutInflateron Android Dev Site for more information about it.

使用 LayoutInflator 将外部布局膨胀到现有布局中。查看Android Dev Site 上的LayoutInflater了解更多信息。