难以在网格布局中布局视图
Difficult to layout views in Grid layout
在我的 android 项目中,我有一个包含三列的网格布局,但是当我向网格添加视图时,它没有在设计器中显示第三列。第一列包含文本视图,第二列包含微调器,第三列包含图像视图。每个视图的宽度设置为 "Wrap_content"。但是第二列中的微调器正在占用剩余的 space 并将第三列推出边界。这是我的 xml
<android.support.v7.widget.GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
app:columnCount="3"
android:columnWidth="auto_fit"
app:columnOrderPreserved="true"
app:orientation="horizontal"
app:rowOrderPreserved="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/title"
android:id="@+id/textView2"
app:layout_gravity="center"
app:layout_column="0"
app:layout_row="0"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
android:id="@+id/spinner"
app:layout_column="1"
app:layout_row="0"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
android:id="@+id/imageView"
android:background="@drawable/ic_astrick"
app:layout_column="2"
app:layout_row="0"/>
</android.support.v7.widget.GridLayout>`
我已尝试为所有视图赋予布局权重,但没有 use.I 附上布局预览。 image view的位置用红色标出
谁能告诉我在网格布局中正确排列视图的方法。
从API21开始,GridLayout引入了'weight'的原理。
documetation link
To make a column stretch, make sure all of the components inside it
define a weight or a gravity. To prevent a column from stretching,
ensure that one of the components in the column does not define a
weight or a gravity.
来自其他答案的示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="@+id/choice_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="4dp"
grid:alignmentMode="alignBounds"
grid:columnCount="2"
grid:rowOrderPreserved="false"
grid:useDefaultMargins="true">
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile1" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile2" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile3" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile4" />
</android.support.v7.widget.GridLayout>
在我的 android 项目中,我有一个包含三列的网格布局,但是当我向网格添加视图时,它没有在设计器中显示第三列。第一列包含文本视图,第二列包含微调器,第三列包含图像视图。每个视图的宽度设置为 "Wrap_content"。但是第二列中的微调器正在占用剩余的 space 并将第三列推出边界。这是我的 xml
<android.support.v7.widget.GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
app:columnCount="3"
android:columnWidth="auto_fit"
app:columnOrderPreserved="true"
app:orientation="horizontal"
app:rowOrderPreserved="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/title"
android:id="@+id/textView2"
app:layout_gravity="center"
app:layout_column="0"
app:layout_row="0"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
android:id="@+id/spinner"
app:layout_column="1"
app:layout_row="0"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
android:id="@+id/imageView"
android:background="@drawable/ic_astrick"
app:layout_column="2"
app:layout_row="0"/>
</android.support.v7.widget.GridLayout>`
我已尝试为所有视图赋予布局权重,但没有 use.I 附上布局预览。 image view的位置用红色标出
谁能告诉我在网格布局中正确排列视图的方法。
从API21开始,GridLayout引入了'weight'的原理。 documetation link
To make a column stretch, make sure all of the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity.
来自其他答案的示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="@+id/choice_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="4dp"
grid:alignmentMode="alignBounds"
grid:columnCount="2"
grid:rowOrderPreserved="false"
grid:useDefaultMargins="true">
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile1" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile2" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile3" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile4" />
</android.support.v7.widget.GridLayout>