2011-04-15 68 views
0

我有一個在構造函數中延伸LinearLayoutlayoutinflater - 父母爲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功能!我的缺點之一

回答

0

您應該嘗試: LinearLayout view =(LinearLayout)inflater.inflate(R.layout.titlebar,null,false);

AFAIK這將返回視圖,其中根是膨脹的XML的根。

+0

沒有那麼我得到一個錯誤:錯誤/ AndroidRuntime(11097):java.lang.RuntimeException:無法啓動活動:android.view.InflateException:二進制XML文件行#13:錯誤充氣類MyClass – passsy 2011-04-15 08:28:32

+0

您可以發佈這個XML文件的內容? – Kocus 2011-04-15 08:38:18

+0

它甚至沒有任何其他的XML工作。 – passsy 2011-04-15 08:45:39

0

你應該嘗試

LayoutInflater inflater = LayoutInflater.from(this); 

,如果你是你的活動類的內部,或

LayoutInflater inflater = LayoutInflater.from(YourActivity.this); 

,如果你在你活動類裏面內嵌類。

然後,如果你想在新的視圖添加到您的其他活動裏(說my_canvas):

//[YourActivity.] is needed when you are in an inline class! 
//RelativeLayout is not mandatory, you can have any other layout there 
final RelativeLayout canvas = (RelativeLayout) [YourActivity.]this.findViewById(R.id.my_canvas); 
final View titleBar = inflater.inflate(R.layout.titlebar, canvas, false); 

現在你可以添加這個標題欄的你需要的地方。

更新

這是充氣方法apidocs:

public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

自:API級別1
充氣從指定的XML資源的新視圖層次結構。如果發生錯誤,則拋出InflateException。

參數
資源 ID爲XML佈局資源加載(例如,R.layout。main_page)
可選視圖是生成的層次結構的父級(如果attachToRoot爲true),或者只是爲返回的層次結構的根提供一組LayoutParams值的對象(如果attachToRoot爲false)。
attachToRoot膨脹層次結構是否應附加到根參數?如果爲false,則root僅用於爲XML中的根視圖創建LayoutParams的正確子類。

返回 充氣層次結構的根視圖。如果提供了root並且attachToRoot爲true,則這是root;否則它就是膨脹的XML文件的根源。

+0

感謝您的回覆。但我不在活動中編碼。我寫了一個像這樣的CompountComponent。 http://stackoverflow.com/questions/1476371/android-writing-a-custom-compound-component – passsy 2011-04-15 10:14:01

+0

然後,你應該只指定一個適當的'LayoutParams'而不是畫布變量的組件。看到我的更新 – rekaszeru 2011-04-15 10:19:18