GridLayout列在较小的屏幕设备上消失

时间:2019-06-05 11:26:50

标签: android android-gridlayout

我在ConstraintLayout中包含的GridLayout遇到了一些问题。在较大的屏幕设备(例如,Pixel 2/3 XL)上,GridLayout看起来很好,所有三列的内容均按预期显示。

在较小的屏幕设备上,只有第一列可见,而其他列似乎完全消失了。我在这里想念什么?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/login_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:animateLayoutChanges="true"
    tools:context=".myactivity.MyActivity">


  <androidx.gridlayout.widget.GridLayout
        android:id="@+id/activity_gridlayout_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:columnCount="3"
        app:layout_constraintTop_toBottomOf="@id/top_layout"
        app:rowCount="1">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/md_keylines_medium"
            android:layout_marginTop="@dimen/md_keylines"
            android:layout_marginEnd="@dimen/md_keylines"
            android:layout_marginBottom="@dimen/md_keylines"
            android:orientation="vertical"
            app:layout_columnWeight="1">

            <ImageView
                android:id="@+id/activity_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_climate" />

            <TextView
                android:id="@+id/activity_state_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/colorWhite" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/md_keylines"
            android:orientation="vertical"
            app:layout_columnWeight="1">

            <com.google.android.material.chip.Chip
                android:id="@+id/activity_temp"
                style="@style/Widget.MaterialComponents.Chip.Action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="22.0"
                app:chipIcon="@drawable/ic_minus"
                app:chipIconSize="22dp"
                app:closeIcon="@drawable/ic_add"
                app:closeIconEnabled="true"
                app:closeIconSize="22dp"
                app:closeIconTint="@color/colorAccent" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/md_keylines"
            android:orientation="vertical"
            app:layout_columnWeight="1">

            <com.google.android.material.chip.Chip
                android:id="@+id/activity_toggle"
                style="@style/Widget.MaterialComponents.Chip.Action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/start"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/colorAccent"
                app:chipIcon="@drawable/ic_power"
                app:chipIconTint="@color/colorWhite" />

        </LinearLayout>
    </androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

设计视图应该是这样

   <com.google.android.material.card.MaterialCardView
            android:id="@+id/tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{(v)->callback.onTabClick(v)}"
            app:touchListener="@{mContext}">

            <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center">

                <ImageView
                    android:id="@+id/iv_card_icon"
                    android:layout_width="@dimen/grid_menu_image"
                    android:layout_height="@dimen/grid_menu_image"
                    android:layout_centerInParent="true"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="16dp"
                    app:imageResource="@{tabIcon}"
                    android:scaleType="center"
                    app:layout_constraintBottom_toTopOf="@+id/tv_guide_title"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_chainStyle="spread"

                    tools:src="@drawable/guide_icon" />

                <!--android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"-->
                <com.hp.pregnancy.customviews.new_50.RobotoMediumTextView
                    android:id="@+id/tv_guide_title"
                    style="@style/TextAppearance.MaterialComponents.Subtitle1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="16dp"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:maxLines="2"
                    android:text="@{tabTitle}"
                    android:textAlignment="center"
                    android:textColor="@color/menu_color"
                    android:textFontWeight="500"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/iv_card_icon"
                    tools:text="@string/guideTitle" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.card.MaterialCardView>

对于网格这样包含

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/relativeLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include

        android:id="@+id/include2"
        layout="@layout/toolbar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"/>

    <include
        android:id="@+id/tab1"
        layout="@layout/card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/small_margin"
        android:layout_marginTop="@dimen/small_margin"
        android:layout_marginEnd="@dimen/xsmall_margin"
        android:layout_marginBottom="@dimen/xsmall_margin"
        app:callback="@{callback}"
        app:layout_constraintBottom_toTopOf="@+id/tab3"
        app:layout_constraintEnd_toStartOf="@+id/guideline7"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/include2"
       />

    <include
        android:id="@+id/tab2"
        layout="@layout/card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/xsmall_margin"
        android:layout_marginEnd="@dimen/small_margin"
        android:layout_marginLeft="@dimen/xsmall_margin"
        android:layout_marginRight="@dimen/small_margin"
        android:layout_marginStart="@dimen/xsmall_margin"
        android:layout_marginTop="@dimen/small_margin"
        app:layout_constraintBottom_toTopOf="@+id/tab4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/guideline7"
        app:layout_constraintTop_toBottomOf="@+id/include2"
       /> <!--guideLayout-->

    <include
        android:id="@+id/tab3"
        layout="@layout/card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/xsmall_margin"
        android:layout_marginEnd="@dimen/xsmall_margin"
        android:layout_marginLeft="@dimen/small_margin"
        android:layout_marginRight="@dimen/xsmall_margin"
        android:layout_marginStart="@dimen/small_margin"
        android:layout_marginTop="@dimen/xsmall_margin"
        app:layout_constraintBottom_toTopOf="@+id/tab5"
        app:layout_constraintEnd_toStartOf="@+id/guideline7"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tab1"
        />

    <include
        android:id="@+id/tab4"
        layout="@layout/card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/xsmall_margin"
        android:layout_marginEnd="@dimen/small_margin"
        android:layout_marginLeft="@dimen/xsmall_margin"
        android:layout_marginRight="@dimen/small_margin"
        android:layout_marginStart="@dimen/xsmall_margin"
        android:layout_marginTop="@dimen/xsmall_margin"
        app:layout_constraintBottom_toTopOf="@+id/tab6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/guideline7"
        app:layout_constraintTop_toBottomOf="@+id/tab2"
       />

    <include
        android:id="@+id/tab5"
        layout="@layout/card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/xsmall_margin"
        android:layout_marginEnd="@dimen/xsmall_margin"
        android:layout_marginLeft="@dimen/small_margin"
        android:layout_marginRight="@dimen/xsmall_margin"
        android:layout_marginStart="@dimen/small_margin"
        android:layout_marginTop="@dimen/xsmall_margin"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline7"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tab3"
      />

    <include
        android:id="@+id/tab6"
        layout="@layout/card"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/xsmall_margin"
        android:layout_marginEnd="@dimen/small_margin"
        android:layout_marginLeft="@dimen/xsmall_margin"
        android:layout_marginRight="@dimen/small_margin"
        android:layout_marginStart="@dimen/xsmall_margin"
        android:layout_marginTop="@dimen/xsmall_margin"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/guideline7"
        app:layout_constraintTop_toBottomOf="@+id/tab4"
       />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果要测试较小的屏幕,请在活动或片段布局中添加scrollview和constraintlayout,然后添加包含。

答案 1 :(得分:0)

只需制作layout width 0dp where you are using column weight并检查:

赞:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/login_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:animateLayoutChanges="true"
    tools:context=".myactivity.MyActivity">


  <androidx.gridlayout.widget.GridLayout
        android:id="@+id/activity_gridlayout_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:columnCount="3"
        app:layout_constraintTop_toBottomOf="@id/top_layout"
        app:rowCount="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/md_keylines_medium"
            android:layout_marginTop="@dimen/md_keylines"
            android:layout_marginEnd="@dimen/md_keylines"
            android:layout_marginBottom="@dimen/md_keylines"
            android:orientation="vertical"
            app:layout_columnWeight="1">

            <ImageView
                android:id="@+id/activity_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_climate" />

            <TextView
                android:id="@+id/activity_state_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/colorWhite" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/md_keylines"
            android:orientation="vertical"
            app:layout_columnWeight="1">

            <com.google.android.material.chip.Chip
                android:id="@+id/activity_temp"
                style="@style/Widget.MaterialComponents.Chip.Action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="22.0"
                app:chipIcon="@drawable/ic_minus"
                app:chipIconSize="22dp"
                app:closeIcon="@drawable/ic_add"
                app:closeIconEnabled="true"
                app:closeIconSize="22dp"
                app:closeIconTint="@color/colorAccent" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/md_keylines"
            android:orientation="vertical"
            app:layout_columnWeight="1">

            <com.google.android.material.chip.Chip
                android:id="@+id/activity_toggle"
                style="@style/Widget.MaterialComponents.Chip.Action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/start"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/colorAccent"
                app:chipIcon="@drawable/ic_power"
                app:chipIconTint="@color/colorWhite" />

        </LinearLayout>
    </androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

希望有帮助。