2015-04-17 103 views
2

我想動態地將一個xml元素添加到TableRow的左側。將XML佈局動態添加到TableRow Android

這是我的xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" > 

    <View 
     android:layout_width="2dp" 
     android:layout_height="match_parent" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="10dp" 
     android:background="@android:color/black" /> 

    <View 
     android:id="@+id/view1" 
     android:layout_width="7dp" 
     android:layout_height="7dp" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="7dp" 
     android:background="@drawable/dot" /> 
</RelativeLayout> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:padding="20dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

它看起來像這樣:enter image description here

我想獲得一個時間表的樣子。我所有的元素都是動態添加的表格行。我需要將這個XML視圖添加到我的表格行的左側。我怎樣才能做到這一點?

編輯: 我試着將它作爲DrawableWithIntrinsicBounds添加到表格的左側。但它看起來像這樣:enter image description here。形狀之間存在差距

+0

將「時間線外觀」設置爲表格行的背景會不會更容易(也更高效)?一個簡單的9補丁就可以解決這個問題。或者,可能需要設置表格行來處理時間軸圖形本身的繪製(這可能會更多工作,但如果需要動態更改其顏色,則更靈活)。 –

+0

@MH。我還沒有使用9補丁。任何示例?我將用這個解決方案與背景無關的原因編輯我的問題。請檢查 –

+0

我同意@MH,他說的是真相,真相和唯一的真相 – pskink

回答

1

您可以使用上面發佈的代碼創建一個名爲:的xml文件。 然後你有一個你的桌子的佈局,但你也有你放在你的桌子上的每個項目的佈局。它可能稱爲類似:table_item.xml

然後,你可以包括到它並確保它是你的左邊:

<include 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    layout="@layout/timeline.xml" /> 
+0

TableRow的所有元素都是動態添加的。所以我想動態地包含它 –

0

創建動態項目佈局文件。 然後在tablerow中創建ViewGroup中的動態視圖,並在代碼中將充氣佈局添加爲它的子項。