在中间布局动态网格 [英] Layout dynamic grid in middle
问题描述
我有一个(当前)宽四个单元格的网格.我指定的每个单元格的宽度为20px.我想将其放置在纵向布局的中间和顶部. 我的代码如下.我的问题是,hte网格似乎填满了整个宽度,而它希望它仅占用列数*仅列宽.并为其周围留有空白. 我在每个组件上都加了颜色,以尝试轻松查看每个组件的位置.第一个LinearLayout中的两个视图从不显示.我已经尝试过
I have a grid with (currently) four cells wide. Each cell I am specifying to be 20px wide. I want to position this in the middle and at the top of a portrait layout. My code is below. My problem is that hte grid seems to fill the whole width whereas it want it to just take number of columns * column width only. And for white space around it. I've put colours on each of the components to try and make it easy to see where each one is. The two views in the first LinearLayout are never shown. Ive tried
<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>
此图显示了我想要的东西.
This image shows the sort of thing I want.
有人可以帮助我调整此xml吗?网格中的列数和行数会有所不同,因此其周围的空间应动态调整大小.
Can anyone help me tweak this xml? The number of columns and rows in the grid will vary so the space around it should dynamically resize.
谢谢
推荐答案
尝试:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:gravity="center"
android:background="#ffff00">
<View
android:layout_width="0dp"
android:background="#ff0000"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<GridView
android:id="@+id/grid_view"
android:background="#ffffff"
android:layout_width="80dp"
android:layout_height="80dp"
android:numColumns="4"
android:columnWidth="20dp"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"/>
<View
android:layout_width="0dp"
android:background="#FFFF00FF"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#FF00FFFF"
android:layout_height="0dp"
android:layout_weight="3"/>
</LinearLayout>
您将获得类似的内容:
顺便说一句:您可以根据需要更改GridView的宽度/高度,也可以使用与旁边的视图类似的方法在其中放置权重.
BTW: you can change the width/height of your GridView as you wish, you can put there a weight too with a similar approach as for the views next to it.
这篇关于在中间布局动态网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!