在我的項目中,我的屏幕中重複了相同的模式 - 它基本上是一個包含標題,圖像和特定背景的線性佈局視圖的容器。爲了避免多次複製和粘貼相同的序列,我認爲我可以創建複合視圖,擴展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>