View disappears after setting height of gridLayout to wrap_content

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 17:08:41

问题


I have a list view. The layout for each row is:

<?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>

What I want to have is this:

But when I change the GridLayout height to wrap_content, the black line disappears. I can see the black line if I set the height to a constant value:

<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">

But I don't want to have a same height for each row. What is the problem?


回答1:


You should change the layout_height of view (black line) to 0 dp and use layout_gravity to 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"
        />


来源:https://stackoverflow.com/questions/32088780/view-disappears-after-setting-height-of-gridlayout-to-wrap-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!