【问题标题】:I want to reduce the space between the cardview in grid adapter我想减少网格适配器中卡片视图之间的空间
【发布时间】:2020-11-19 18:15:55
【问题描述】:

这是 xml 代码。实际上,在运行此代码时,我期望的输出或布局并没有出现,而是显示了一些不同的输出。我还附上了下面输出的屏幕截图。

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="191dp"
        android:layout_height="170dp"
        app:cardElevation="10dp"
        app:contentPadding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.247"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="35dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="166dp"
            android:layout_height="146dp">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="151dp"
                android:layout_height="108dp"
                tools:layout_editor_absoluteX="6dp"
                tools:layout_editor_absoluteY="7dp"
                tools:srcCompat="@tools:sample/avatars" />

            <TextView
                android:id="@+id/textView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="first"
                android:textSize="20dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.503"
                app:layout_constraintStart_toStartOf="parent"
                tools:layout_editor_absoluteY="122dp" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

这些是我的输出快照。

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    尝试使用根视图的android:layout_height="wrap_content" 而不是match_parent

    另外,在生产中使用tools:ignore="MissingConstraints" 也是不对的,那会错过你的视图;而是硬编码缺少的约束。

    我已经在下面解决了这个问题

    <?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:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <androidx.cardview.widget.CardView
            android:layout_width="191dp"
            android:layout_height="170dp"
            app:cardElevation="10dp"
            app:contentPadding="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="166dp"
                android:layout_height="146dp">
    
                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="151dp"
                    android:layout_height="108dp"
                    app:layout_constraintBottom_toTopOf="@id/textView15"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <TextView
                    android:id="@+id/textView15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="first"
                    android:textSize="20dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/imageView4" />
                
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>
        
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多