我有一个网格(当前)有四个单元格宽。我指定的每个单元格宽度都是20px。我想把它放在肖像布局的中间和顶部。 我的代码如下。我的问题是,网格似乎填满了整个宽度,而它希望它只需要列数*列宽度。并为它周围的空白空间。 我已经把颜色放在每个组件上去尝试,并且很容易看到每个组件的位置。第一个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吗?网格中的列数和行数会有所不同,因此它周围的空间应该动态调整大小。
这是一个exerpt?您至少缺少另一个线性布局作为所有这些视图(垂直) – user3802077
的父级,您不需要引力(它会影响视图内容而不影响其排列),Gridview周围的视图应具有固定高度或match_parent高度。此外你的linearlayout应该使用全屏的高度,因为这是你想要的:)(match_parent) – user3802077
感谢您的回复。我已经更新了代码片段。我删除了graivty并添加了LinearLayout。不知道复制和粘贴发生了什么。对不起 – RNJ