Android 当我从xml膨胀时,为什么mLayoutParams为空?
这是膨胀视图的代码:Android 当我从xml膨胀时,为什么mLayoutParams为空?,android,android-layout,android-widget,Android,Android Layout,Android Widget,这是膨胀视图的代码: LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.test, null); 还有我的test.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.test, null);
还有我的test.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="50px">
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
我在这行下面设置了断点,它膨胀test.xml
,发现视图中的mLayoutParams
实例为空。我想如果在第二个参数充气时指定父视图,则不会发生这种情况。但感觉还是很奇怪:如果
mLayoutParams
为空,那么我为什么要在LinearLayout元素中设置layout\u width
和layout\u height
属性。使用以下方法创建
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
使用此代码
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务)
或
LayoutInflater充气器=(LayoutInflater)getLayoutInflater()
在这之后,我想你的问题会得到解决。
http://androidforums.com/developer-101/299916-inflating-layout-xml-files-layout-parameters.html
我发现以前有人问过这个问题,但没有人回答。你明白了吗?我试着用这种方法来实例化充气机,但是mLayoutParams仍然是空的,这是我的错。我把“mLayoutParams”输入错了“MLAYOUTPLATE”。请再检查一遍。