在onCreateView中加载布局以获取自定义首选项

mic*_*ael 10 layout android preferences

我可以在onCreateView调用期间从xml加载布局,我试过:

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

它崩溃了,我不知道从方法参数返回ViewGroup父类的内容是什么?我也尝试过:

  • View view = View.inflate(getContext(),R.layout.test,parent)但它没有用.

我将root布局命名为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 ....
Run Code Online (Sandbox Code Playgroud)

你能告诉我我做错了什么还是指出了一些有用的例子.谢谢

更新

以下是关于如何对上述布局进行充气的解决方案:

    LayoutInflater inflater =  (LayoutInflater)getContext().
                              getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.test, parent, false);
Run Code Online (Sandbox Code Playgroud)

Dan*_*lle 2

如果您正在使用,onCreateView我猜您正在使用 afragment或扩展 a View。无论如何检查一下:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.your_layout, container);
}
Run Code Online (Sandbox Code Playgroud)