12

1 番目と 2 番目の 2 つのレイアウトがあり、2 番目から 1 番目に挿入したいと考えています。Get Layoutボタンを押すと、ID @ + id/layoutを持つレイアウトに2番目のレイアウトを挿入したいのですが、ボタンの下部に2番目のレイアウトが表示されます

最初のレイアウト

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

2 番目のレイアウト

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

5 に答える 5

33

私があなたを正しく理解していれば、最初のレイアウト内で次のコードを使用する必要があります

<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);
于 2012-08-22T11:18:37.253 に答える
12
    LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
    getLayoutInflater().inflate(R.layout.second_layout, placeHolder);
于 2012-08-22T11:20:07.357 に答える
9

使用する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>
于 2017-01-06T04:17:11.513 に答える
0

LayoutInflator を使用して、外部レイアウトを既存のレイアウトにインフレートします。詳しくは、Android Dev Site の LayoutInflater をご覧ください。

于 2012-08-22T11:17:19.043 に答える