android 在子级和父级中都使用布局高度 0dp

标签 android android-layout

我想实现以下目标:

enter image description here

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.42">

        <ListView 
            android:id="@+id/ist"
            android:layout_width="match_parent"
            android:layout_height="0dp" <!-- GETTING ERROR : Suspicious size: this will make the view invisible, probably intended for layout_width -->
            android:layout_weight="1"
            android:background="@color/Bkgd"
            android:transcriptMode="alwaysScroll"
            android:stackFromBottom="true" />

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:background="@android:color/white" >

            <Button android:id="@+id/Btn"                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/input_btn"
                android:background="@android:color/white" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.58"
        android:background="@color/colorPrimary" >

        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:columnWidth="30dp"
            android:numColumns="auto_fit"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center" />

    </LinearLayout>

</LinearLayout>

我在上面的代码中标记了 Android Studio 显示布局错误的地方。

使用上面的布局,我得到的输出是:

enter image description here

我应该做哪些更改才能使布局像第一张图片一样?

最佳答案

我们在线性布局方面遇到了令人惊讶的困难,所以如果您不反对相对布局,这可以做类似的事情。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="0.42">

        <ListView
            android:id="@+id/ist"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".8"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/linearLayoutHolder"
            android:background="@color/Bkgd"
            android:transcriptMode="alwaysScroll"
            android:stackFromBottom="true" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/linearLayoutHolder"
            android:layout_alignParentBottom="true"
            android:layout_weight=".2"
            android:background="@android:color/white" >

            <Button android:id="@+id/Btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/input_btn"
                    android:background="@android:color/white" />

        </LinearLayout>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.58"
        android:background="@color/colorPrimary" >

        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:columnWidth="30dp"
            android:numColumns="auto_fit"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center" />

    </LinearLayout>

</LinearLayout>

关于android 在子级和父级中都使用布局高度 0dp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32786162/

相关文章:

android - Imageview Interactive-water-effect android

java - 获取 java.lang.IndexOutOfBoundsException : Index: 0, Size: 0 in react native

android - 在 kotlin 中,如何创建个人资料图像并在离线短信应用程序中将联系人姓名的第一个字母显示为个人资料图像?

android - Model-View-Presenter 和 Android 应用程序设计

android - 如何获取指定位置的URI?

android - 如何在Android中使用YouTube API获取通过关键字搜索的相关视频列表

Android WebView 不会加载本地文件

Android AppCompatCheckbox 填充/边距/重力

android - 如何在操作栏的多个菜单项中同时显示图标和文本?

android - processCommandApdu 方法中 HostApduService 的问题