0

私は次のことがxmlで達成できるかどうか疑問に思っていました:

私は次のようなこの単純なレイアウトを持っています:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="Address: "
    android:textSize="14dp" />

</LinearLayout>

このレイアウトを他のxmlに含めると、別のTextViewを追加できるので、結果は次のようになります。

アドレス:別のテキストビューのテキスト。

プログラムではなく、xmlでこれを実行したいと思います。

私は以下を試しました:

            <include layout="@layout/mybaselayout" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="mylocalizedstring" />
            </include>

それは機能していません、私はここでxmlで行うことが不可能なことを試みていますか?

ありがとう。

4

5 に答える 5

1

xmlに含まれるレイアウト内にビューを追加する方法は不可能のようです。それを実現する唯一の方法は、プログラムで行うことです。

于 2012-08-01T04:38:14.577 に答える
0
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
          android:orientation="vertical"  
          android:layout_width="fill_parent"  
          android:layout_height="fill_parent"  
    >  
<TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="This is a 'sans' demo!"  
        android:typeface="sans"  
        />  
 <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="This is a 'serif' demo!"  
        android:typeface="serif"  
        />  
 <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="This is a 'monospace' demo!"  
        android:typeface="monospace"  
        />  

  </LinearLayout>  
于 2012-07-31T06:37:55.687 に答える
0

レイアウトファイルに直接追加TextViewしたくない場合は、コーディングを使用する必要があります。xmlレイアウトを複数回含め、数回だけ追加したい場合は、ViewStubTextViewを使用してください。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Address: "
        android:textSize="14dp" />
    <ViewStub
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/view_stub"
        android:inflatedId="@+id/addr"
        android:layout="@layout/text_view" />
</LinearLayout>

text_view.xmlレイアウトファイル

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />

表示したいときはTextView膨らませます

TextView text_view = (TextView) ((ViewStub) findViewById(R.id.view_stub)).inflate();
text_view.setText(...);
于 2012-07-31T06:50:07.943 に答える
0

あなたは確かにあなたの2番目のxmlファイルでこの方法を行うことができます、

<RelativeLayout>
<include android:id="@+id/llone" layout="@layout/topbar"/>
<TextView android:layout_toRightOf="@id/llone"></TextView>
</RelativeLayout>
于 2012-07-31T06:50:56.613 に答える
-1

<include>タグを使用できます

<include layout="@layout/topbar" android:layout_width="wrap_content" android:layout_height="wrap_content" />
于 2012-07-31T06:40:51.060 に答える