Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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布局根视图中指定高度失败_Android - Fatal编程技术网

Android 在xml布局根视图中指定高度失败

Android 在xml布局根视图中指定高度失败,android,Android,假设我有一个自定义布局xml,并将其根视图的高度设置为300dp <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height

假设我有一个自定义布局xml,并将其根视图的高度设置为300dp

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="300dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is text"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click me"/>

</LinearLayout>

但是,customView的高度不是300dp。我哪里做错了?

尝试使用android:minHeight=300dp,然后android将确保最小300dp。

我认为这是因为您对Textview和按钮高度使用了换行内容,所以布局高度不能是300dp,因为缺少一些空格。您还应该定义属性高度,而不是包装内容

我通常使用“匹配父对象”作为布局高度,并设置内部属性的高度

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="15dp"
    android:text="This is text"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="15dp"
    android:text="click me"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="15dp"
    android:text="This is text"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="15dp"
    android:text="click me"/>

</LinearLayout>