我想实现以下目标:
<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 显示布局错误的地方。
使用上面的布局,我得到的输出是:
我应该做哪些更改才能使布局像第一张图片一样?
最佳答案
我们在线性布局方面遇到了令人惊讶的困难,所以如果您不反对相对布局,这可以做类似的事情。
<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/