【问题标题】:how to insert layout to layout in android?如何在android中将布局插入布局?
【发布时间】:2012-08-17 19:12:16
【问题描述】:

我有两个布局,第一个和第二个,我想插入第二个到第一个。 我想将第二个布局插入到具有 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>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    使用 LayoutInflator 将外部布局膨胀到现有布局中。在 Android 开发网站上查看 LayoutInflater 以了解更多信息。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:
            LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
            getLayoutInflater().inflate(R.layout.second_layout, placeHolder);
        

        【讨论】:

        • 如果我需要动态插入多个布局(布局编号事先不知道),我该怎么做?
        【解决方案4】:

        使用include

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

        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>
        

        【讨论】:

          【解决方案5】:

          你应该使用这段代码 sn-p 在另一个布局中添加一个布局。

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

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

          【讨论】:

            猜你喜欢
            • 2021-09-23
            • 2021-08-10
            • 2015-03-21
            • 2012-09-11
            • 2017-06-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-23
            相关资源
            最近更新 更多