如何在GridLayout中模仿RelativeLayout参数

时间:2014-03-14 18:05:16

标签: android android-layout android-gridlayout

我正在尝试使用GridLayout重新编写我的一个布局,并且有点遇到其中一个问题。这就是我现在所拥有的:

grid layout

这是我的布局:

<GridLayout
    style="@style/Widget.Card"
    android:layout_width="match_parent"
    android:layout_height="@dimen/card_height"
    android:background="@color/news_card_background"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:columnCount="3"
    android:rowCount="2" >

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="@dimen/card_thumbnail_width"
        android:layout_height="match_parent"
        android:layout_rowSpan="2"
        android:background="@drawable/news_card_thumbnail_background"
        android:contentDescription="@string/thumbnail"
        android:cropToPadding="true"
        android:scaleType="centerCrop"
        android:visibility="visible" />

    <com.inrix.twc.view.RobotoTextView
        android:id="@+id/title"
        style="@style/TextAppearenceNewsTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/news_title_horizontal_margin"
        android:layout_marginRight="@dimen/news_title_horizontal_margin"
        android:text="Title" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_rowSpan="2"
        android:src="@drawable/audio_mrss"
        tools:ignore="ContentDescription" />

    <com.inrix.twc.view.RobotoTextView
        android:id="@+id/footer"
        style="@style/TextAppearenceNewsFooter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="@dimen/news_description_horizontal_margin"
        android:layout_marginRight="@dimen/news_description_horizontal_margin"
        android:text="description" />
</GridLayout>

但是当其中一个文本视图很长时,它会将我的最后一列推出屏幕:

grid layout 2

问题是 - 如何限制文字超出行的范围,因此我的图标始终保持右对齐。所以基本上我试图模仿RelativeLayout的{​​{1}}属性。

我意识到我可以轻松地使用toLeftOf来做到这一点,但由于其他原因,它对我不起作用。我希望可以使用RelativeLayout

1 个答案:

答案 0 :(得分:2)

这不是gridlayout的目标。我不认为这甚至(轻松)成为可能。阻止GridLayout限制。

  

GridLayout不提供权重原则的支持,如权重中所定义。通常,因此无法配置GridLayout以在多个组件之间分配多余空间。   然而,一些常见的用例可以如下容纳。在单元组中的组件周围放置等量的空间;使用CENTER对齐(或重力)。用于完全控制行或列中的多余空间分布;使用LinearLayout子视图来保存关联单元组中的组件。使用这些技术时,请记住可以将单元格组定义为重叠。

TextView需要权重来在屏幕上定义自己的大小(使用换行内容)。

您可以尝试使用相对布局或构建一个linearlayout树,如下所示:

  • LinearLayout水平
    • ImageView @thumbnail
    • LinearLayout垂直重量1
      • TextView @title
      • TextView @footer
    • ImageView @icon