GridLayout 未均匀填充行 (Android)
GridLayout not filling rows evenly (Android)
我正在尝试使用 GridLayout (API 21) 获得均匀填充的网格。使用 layout_columnWeight 属性在水平方向上填充效果很好。与 layout_rowWeight 属性相同失败(见屏幕截图)。我很无能。似乎这两个属性的工作方式不同。
更简化的布局也显示相同的行为(1 行 x 2 列有效,2 行 x 1 列失败)。还明确添加 layout_row 和 layout_column 属性不会改变任何东西。
请不要回答"use linearLayout"。我想让它与 GridLayout 一起工作。
<FrameLayout 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:background="#0099cc"
tools:context=".Locomotion">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="north west"
android:id="@+id/textViewNW"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#fe4141" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="north east"
android:id="@+id/textViewNE"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#51f328" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="south west"
android:id="@+id/textViewSW"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#fefe00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="south east"
android:id="@+id/textViewSE"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#0080f0" />
</GridLayout>
</FrameLayout>
提前感谢任何提示!
托马斯
这很难。必须发挥创意来解决它。基本上我的解决方案是为每个 col 嵌套 GridLayouts 和 1 行的主 GridLayout。此外,每个 col GridLayout 必须位于 orientation="verticle" 和 width/height = "wrap_content"
结果:
代码如下:
<FrameLayout 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:background="#0099cc"
tools:context=".Locomotion">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="1">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="2"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:orientation="vertical">
<TextView
android:id="@+id/textViewNW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#fe4141"
android:text="north west"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textViewSW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#fefe00"
android:text="south west"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="2"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:orientation="vertical">
<TextView
android:id="@+id/textViewNE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#51f328"
android:text="north east"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textViewSE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#0080f0"
android:text="south east"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</GridLayout>
</FrameLayout>
我正在尝试使用 GridLayout (API 21) 获得均匀填充的网格。使用 layout_columnWeight 属性在水平方向上填充效果很好。与 layout_rowWeight 属性相同失败(见屏幕截图)
更简化的布局也显示相同的行为(1 行 x 2 列有效,2 行 x 1 列失败)。还明确添加 layout_row 和 layout_column 属性不会改变任何东西。
请不要回答"use linearLayout"。我想让它与 GridLayout 一起工作。
<FrameLayout 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:background="#0099cc"
tools:context=".Locomotion">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="north west"
android:id="@+id/textViewNW"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#fe4141" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="north east"
android:id="@+id/textViewNE"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#51f328" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="south west"
android:id="@+id/textViewSW"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#fefe00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="south east"
android:id="@+id/textViewSE"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="center|fill"
android:background="#0080f0" />
</GridLayout>
</FrameLayout>
提前感谢任何提示!
托马斯
这很难。必须发挥创意来解决它。基本上我的解决方案是为每个 col 嵌套 GridLayouts 和 1 行的主 GridLayout。此外,每个 col GridLayout 必须位于 orientation="verticle" 和 width/height = "wrap_content"
结果:
代码如下:
<FrameLayout 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:background="#0099cc"
tools:context=".Locomotion">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="1">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="2"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:orientation="vertical">
<TextView
android:id="@+id/textViewNW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#fe4141"
android:text="north west"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textViewSW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#fefe00"
android:text="south west"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="1"
android:rowCount="2"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:orientation="vertical">
<TextView
android:id="@+id/textViewNE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#51f328"
android:text="north east"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textViewSE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:background="#0080f0"
android:text="south east"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</GridLayout>
</FrameLayout>