开发者

Loading layout in onCreateView for custom preference

Can I load layout from xml during onCreateView call, I tried:

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

it crashes and I don’t know what to return a ViewGroup parent from a method arguments? I also tried:

  • View view = View.inflate(getContext(), R.layout.test, parent) but it didn’t worked as well.

I named root layout widget_frame but it didn’t helped

<?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 ....

Could you tell me what I开发者_开发知识库’m doing wrong or point me to some working example. Thanks

Update

Below is a woking solution on how to inflate above layout:

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


If you are using onCreateView I guess you are either using a fragment or extending a View. Anyway check this:

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


If you are inflating a layout you should do something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜