如何扩展行?

时间:2012-12-21 19:45:03

标签: android android-layout android-gridlayout

我正在尝试使用GridLayout并且无法扩展行(使用TextViews)来“填充”布局中的空间。

垂直图片(注意红线是行应该展开的位置):    enter image description here

横向图片:
enter image description here

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:columnCount="2"
  android:useDefaultMargins="true" >
<TextView
    style="@android:style/TextAppearance.Large"
    android:layout_columnSpan="2"
    android:layout_gravity="center_horizontal"
    android:padding="8dp"
    android:text="Which means: lacking in originality as to be obvious and boring." />
<Space android:layout_height="24dp" android:layout_columnSpan="2" />
<TextView style="@style/AnswerLetter" android:text="A." />
<TextView style="@style/AnswerChoice" android:text="ascetic" />
<TextView style="@style/AnswerLetter" android:text="B." />
<TextView style="@style/AnswerChoice" android:text="banal" />
<TextView style="@style/AnswerLetter" android:text="C." />
<TextView style="@style/AnswerChoice" android:text="contraption" />
<TextView style="@style/AnswerLetter" android:text="D." />
<TextView style="@style/AnswerChoice" android:text="dominion" />
<TextView style="@style/AnswerLetter" android:text="E." />
<TextView style="@style/AnswerChoice" android:text="enervation" />
</GridLayout>

styles.xml:

 <style name="AnswerLetter" parent="@android:style/TextAppearance.Medium">
   <item name="android:layout_gravity">right</item>
   <item name="android:paddingLeft">32dp</item>
   <item name="android:textStyle">bold</item>
   <item name="android:gravity">center_vertical|center_horizontal</item>
 </style>
 <style name="AnswerChoice" parent="@android:style/TextAppearance.Large">
   <item name="android:layout_gravity">left</item>
   <item name="android:paddingLeft">32dp</item>
   <item name="android:gravity">left</item>
 </style>

2 个答案:

答案 0 :(得分:6)

您希望所有子视图增长(高度)以填充额外空间,但这在GridLayout中是不可能的:

  

GridLayout不提供权重原则的支持,如权重中所定义。通常,因此不可能将GridLayout配置为在多个行或列之间以非平凡的比例分配多余的空间...以完全控制行或列中的多余空间分布;使用LinearLayout子视图来保存关联单元格组中的组件。“

尝试添加线性布局作为子视图,并将所有子项的权重属性设置为相同的值(因此它们会相等地扩展)。

类似的问题: GridLayout (not GridView) how to stretch all children evenly

答案 1 :(得分:3)

如果我理解正确 - 这种布局可以解决您的问题。 GridView无法拉伸列高,因此您可以使用LinearLayout。 我为你做了这个布局,可能需要一些填充调整

horisontal vertial

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/LinearLayout1"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        style="@android:style/TextAppearance.Large"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:text="Which means: lacking in originality as to be obvious and boring." />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="A." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="ascetic" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            style="@style/AnswerLetter"
            android:text="B." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="banal" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="C." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="contraption" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="D." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="dominion" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="E." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="enervation" />
    </LinearLayout>
</LinearLayout>