2012-07-31 52 views
0

我在想,如果以下可以在XML來實現:是否有可能將視圖添加到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中它,所以結果將如下所示:

地址:另一個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做什麼?

謝謝。

+0

如果我沒有弄錯,你應該可以把textview放在當前的下方。 – sbrichards 2012-07-31 06:36:33

+0

然後,你應該添加textView到這個佈局......但我會說,以編程方式會更容易做到這一點。 – yahya 2012-07-31 06:36:39

+0

但我有這樣的情況,就像有40個本地化的字符串用於第二個文本,但第一個文本總是相同的。所以我試圖使用更少的代碼。 – Niko 2012-07-31 06:41:20

回答

1

看起來像我想添加視圖內包含的佈局在XML中的方式是不可能的,唯一的方式來實現它,是以編程方式。

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

您可以使用<include>標籤

<include layout="@layout/topbar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
+0

是的,我包括該佈局,但我也想在XML中添加另一個TextView。 – Niko 2012-07-31 06:42:34

0

你將不得不使用一些編碼,如果你不想直接添加TextViewxml佈局文件。如果您不止一次地包含您的佈局,並且您只想添加TextView,則只有一些時間使用ViewStub

<?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(...); 
0

它充氣,你一定能做到這樣,在你的第二個XML文件,

<RelativeLayout> 
<include android:id="@+id/llone" layout="@layout/topbar"/> 
<TextView android:layout_toRightOf="@id/llone"></TextView> 
</RelativeLayout> 
相關問題