2013-03-18 82 views
3

在我的項目中,我的屏幕中重複了相同的模式 - 它基本上是一個包含標題,圖像和特定背景的線性佈局視圖的容器。爲了避免多次複製和粘貼相同的序列,我認爲我可以創建複合視圖,擴展LinearLayout並定義所有「樣式」,然後在佈局中使用該組件。 我遵循howto和例子,並讓我的複合視圖起作用。然而,我所看到的所有例子都使用如下所得到的視圖:在XML中定義的擴展LinearLayout的子項不顯示

<com.myproject.compound.myview 
    ...some attrs... 
/> 

即,沒有孩子通過XML添加。我需要使用這樣的:

<com.myproject.compound.myview 
    ...some attrs...> 
    <TextView 
     ..../> 
    ...other views... 

</com.myproject.compound.myview> 

由於我伸出的LinearLayout我期待「MyView的」標籤一樣的LinearLayout工作過,但由於某種原因,項目我把裏面不拿得出。有什麼我需要專門去做內部視圖來繪製?

我的擴展的LinearLayout很簡單,我不覆蓋任何方法,只是調用構造函數超級膨脹這樣的佈局:

LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.my_compound_view, this, true); 

更新:我想我會添加XML作爲一個點參考:

<?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:orientation="vertical" 
android:background="@drawable/bg" 
android:padding="12dp"> 
<TextView 
    android:id="@+id/section_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FF0000AA"  /> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="2dp" 
     android:src="@drawable/line"  /> 
</LinearLayout> 

回答

10

實際上找到了更優雅的解決方案。只需要在複合視圖中使用合併標記而不是LinearLayout。一切歸結爲:

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    <TextView 
     android:id="@+id/section_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="HEADING" 
     android:textColor="#FF0000AA"  /> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:src="@drawable/line"  /> 
</merge> 

​​

主要佈局:

<com.testbench.CompoundLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#FFFFDDEE" 
    android:orientation="vertical"> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Inner text" 
      android:layout_gravity="center_horizontal"/>   
</com.testbench.CompoundLayout> 
6

在閱讀了Android源代碼和例子之後,我想我想到了這一點。基本上在我的情況下,它是複合視圖和自定義佈局的混合。 「複合視圖」部分負責佈置和繪製指定「容器」的XML的內容。但是那個容器內的物品稍後會膨脹,在我的實施中它們並沒有被佈置。 一種方法是遵循自定義佈局路徑 - 我必須實現onLayout()和onMeasure()來正確計算我的孩子(並且在我的研究期間,我做了,它工作)。但是因爲我真的不需要任何與LinearLayout已經不同的東西,並且我不想複製/粘貼它的代碼(這些方法非常龐大),我只是決定覆蓋onFinishInflate()方法並添加了「容器視圖「 那裏。這裏是整個代碼,請評論是可以改進的。

public class CompoundLayout extends LinearLayout{ 
View mView; 
public CompoundLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     mView = inflater.inflate(R.layout.compound_layout, this, false); 
} 

@Override 
public void onFinishInflate(){ 
    super.onFinishInflate(); 
    addView(mView, 0); 
} 
} 

然後在Activity的主佈局中,我只用我的自定義佈局,就像使用LinearLayout一樣。它呈現的是相同的,但總是與那些TextView和ImageView在頂部。