【发布时间】:2016-08-16 11:46:43
【问题描述】:
我有一个(当前)有四个单元格宽的网格。我指定的每个单元格为 20px 宽。我想把它放在纵向布局的中间和顶部。 我的代码如下。我的问题是 hte 网格似乎填满了整个宽度,而它只希望它只占用列数 * 列宽。对于它周围的空白区域。 我已经在每个组件上添加了颜色,以便轻松查看每个组件的位置。第一个 LinearLayout 中的两个视图从未显示。我试过了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".EntryPointFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:background="#FFFFFF00"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:background="#FF000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
android:columnWidth="30dp"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:gravity="center"/>
<View
android:layout_width="wrap_content"
android:background="#FFFF00FF"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#FF00FFFF"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_weight="1"/>
</LinearLayout>
这张图片显示了我想要的那种东西。
谁能帮我调整这个xml?网格中的列数和行数会有所不同,因此其周围的空间应动态调整大小。
【问题讨论】:
-
这是摘录吗?作为所有这些视图的父级,您至少缺少另一个线性布局(垂直)
-
你不需要重力(它影响视图内容而不是它的排列),gridview 周围的视图应该有一个固定或 match_parent 的高度。你的线性布局也应该使用全屏的高度,因为这是你想要的:) (match_parent)
-
感谢您的回复。我现在更新了代码 sn-p。我删除了重力并添加了线性布局。不知道复制和粘贴发生了什么。对不起
标签: android android-layout gridview layout android-linearlayout