Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当我从xml膨胀时,为什么mLayoutParams为空?_Android_Android Layout_Android Widget - Fatal编程技术网

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”。请再检查一遍。