将gridLayout的高度设置为wrap_content后,视图消失

时间:2015-08-19 07:12:35

标签: android android-layout layoutparams android-gridlayout

我有一个列表视图。每行的布局是:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    app:columnCount="4">

    <View
        android:layout_width="3dp"
        android:layout_height="wrap_content"
        app:layout_row="0"
        app:layout_column="1"
        app:layout_rowSpan="3"
        android:background="@color/background_floating_material_dark"
        />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        app:layout_row="0"
        app:layout_column="3"
        app:layout_gravity="center_vertical"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_row="0"
        app:layout_column="2"
        android:maxWidth="210dp"
        android:singleLine="true"
        app:layout_gravity="right|center_vertical"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:ellipsize="end"
        android:layout_margin="10dp"
        android:text="This is title"
        />


    <TextView
        android:id="@+id/start_time_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_gravity="left|center_vertical"
        app:layout_row="0"
        app:layout_column="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        android:text="۱۱ ق.ظ"
        android:paddingRight="5dp"
        />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_gravity="right|center_vertical"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/button_material_dark"
            android:layout_margin="6dp"
            app:layout_row="1"
            app:layout_column="2"
            android:text="subtitle1" />
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="25dp"
            android:layout_height="25dp"
            app:layout_gravity="center_horizontal|center_vertical"
            android:src="@drawable/ic_supervisor_account_black_24dp"
            app:layout_row="1"
            app:layout_column="3"/>

</android.support.v7.widget.GridLayout>

我想要的是:

enter image description here

但是当我将GridLayout高度更改为wrap_content时,黑线会消失。如果我将高度设置为恒定值,我可以看到黑线:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    app:columnCount="4">

但我不希望每排都有相同的高度。有什么问题?

1 个答案:

答案 0 :(得分:2)

您应该将layout_height视图(黑线)更改为0 dp,并使用layout_gravity更改为fill

 <View
        android:layout_width="1dp"
        android:layout_height="0dp"
        app:layout_row="0"
        app:layout_column="1"
        app:layout_rowSpan="3"
        app:layout_gravity="fill"
        android:background="@color/dim_foreground_disabled_material_dark"
        />