我有一個在構造函數中延伸LinearLayout
layoutinflater - 父母爲null
類我稱之爲
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, this, true);
我可以在此視圖訪問的觀點,但不繪製。我認爲原因是這個視圖的mParent仍然是空的。但爲什麼?在Partent應該是這樣的(它擴展的LinearLayout類)
這裏的XML:
<RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="35px"
android:layout_width="fill_parent"
android:background="@drawable/titlebar_bg">
<TextView android:layout_width="wrap_content"
android:textColor="#000000"
android:text="test"
android:id="@+id/title"
android:layout_alignParentTop="true"
android:layout_marginLeft="10pt"
android:layout_height="wrap_content"
android:textSize="10pt"
android:layout_marginTop="3dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="10:09 AM"
android:id="@+id/time"
android:textColor="#000000"
android:layout_alignParentRight="true"
android:layout_marginRight="5px"
android:layout_marginTop="3px"
android:textSize="10pt"></TextView></RelativeLayout>
SOLUTION
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content"
android:textColor="#000000"
android:text="test"
android:id="@+id/title"
android:layout_alignParentTop="true"
android:layout_marginLeft="10pt"
android:layout_height="wrap_content"
android:textSize="10pt"
android:layout_marginTop="3dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="10:09 AM"
android:id="@+id/time"
android:textColor="#000000"
android:layout_alignParentRight="true"
android:layout_marginRight="5px"
android:layout_marginTop="3px"
android:textSize="10pt"></TextView>
使用合併標記和使用XML標籤。
<package.Titlebar
android:id="@+id/testtitlebar"
android:layout_height="35px"
android:layout_width="fill_parent"
android:background="@drawable/titlebar_bg"></package.Titlebar>
這是它的外觀的類:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.titlebar, this);
沒有onLayout功能!我的缺點之一
沒有那麼我得到一個錯誤:錯誤/ AndroidRuntime(11097):java.lang.RuntimeException:無法啓動活動:android.view.InflateException:二進制XML文件行#13:錯誤充氣類MyClass – passsy 2011-04-15 08:28:32
您可以發佈這個XML文件的內容? – Kocus 2011-04-15 08:38:18
它甚至沒有任何其他的XML工作。 – passsy 2011-04-15 08:45:39