2011-01-19 68 views
10

我可以onCreateView通話過程中加載從XML佈局,我想:加載佈局定製偏好

  • setLayoutResource(R.layout.test);
  • setWidgetLayoutResource(R.layout.test);

它崩潰,我不知道從方法參數返回ViewGroup父項是什麼? 我也試過:

  • 查看視圖= View.inflate(的getContext(),R.layout.test,父母),但它沒有工作過。

我命名爲根佈局widget_frame但它並沒有幫助

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:padding="1dp" android:clickable="false" android:id="@+id/widget_frame"> 
<LinearLayout .... 

你能告訴我什麼,我做錯了或點我一些工作的例子。 感謝

更新

下面是如何膨脹上述佈局的沃金的解決方案:

LayoutInflater inflater = (LayoutInflater)getContext(). 
           getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.test, parent, false); 
+0

太好了!這也適用於我!但是文檔有點令人困惑,因爲它說: 「默認行爲是膨脹此首選項的主佈局(請參閱setLayoutResource(int)。」我只是沒有得到調用setLayoutResource的目的是什麼,因爲我們顯然需要返回一個View並且setLayoutResource返回void – Bilthon 2011-12-17 16:24:49

回答

2

如果您正在使用onCreateView我猜你要麼使用fragment或延伸的View。 反正檢查:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.your_layout, container); 
} 
0

如果您正在充氣的佈局,你應該做這樣的事情:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.your_layout, null); 
}