在onCreateView调用期间,我能否从XML中加载布局?我尝试了:
- setLayoutResource(R.layout.test);
- setWidgetLayoutResource(R.layout.test);
但是这会导致程序崩溃,我不知道从方法参数中返回一个ViewGroup parent。 我还尝试过:
- View view = View.inflate(getContext(), R.layout.test, parent),但它也没有起作用。
我将根布局命名为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);
setLayoutResource
来设置布局,你的视图ID应该是"@android:id/widget_frame"
而不是"@+id/widget_frame"
。 - GDanger