【问题标题】:Padding being added to the top of CardView the longer the text in a TextView becomes将填充添加到 CardView 的顶部,TextView 中的文本变得越长
【发布时间】:2019-11-04 19:11:16
【问题描述】:

我在 CardView (CV) 内部的 ConstraintLayout (CL) 中有一个 TextView。 CL 是 CV 中唯一的元素,CL 内部有几个不同的 ImageView 和 TextView。

文本和图像通过 RecyclerViewAdapter 和 ViewHolder 以编程方式填充。当最后一个 TextView 中的文本变得足够长时,填充开始出现在卡片视图的顶部。

带有测试字符串的布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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/viewStopScheduleItem_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dp"
    android:clickable="true"
    android:focusable="true"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="0dp"
    app:cardElevation="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/viewStopScheduleItem_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <ImageView
            android:id="@+id/viewStopScheduleItem_barImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/status_bar_item_background"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp" />

        <ImageView
            android:id="@+id/viewStopScheduleItem_busImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_directions_bus_white"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginBottom="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeShortNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginTop="0dp"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginLeft="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginTop="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_stopTime1TextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM"
            android:textAlignment="viewStart"
            android:layout_gravity="start"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"/>

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

字符串越长,填充出现的越多。

我的尝试:

  • 将 CV 和 CL 的填充和边距设置为零,
  • 弄乱了所有元素的高度和文本视图的高度越小,填充变得越小,但整个字符串不可见。
  • 将视图和布局的高度设置为 match_parent 和 wrap_content 没有成功。
  • 将 CV 和 CL 的重力设置为 start 和 top。

那么,我该如何摆脱填充?

我附上了设计的截图

导致填充的长字符串

没有填充的短字符串

【问题讨论】:

  • 您要删除 padding-top 吗?

标签: android android-layout android-constraintlayout android-cardview


【解决方案1】:

如果您想删除padding_top,您必须像这样更改您的layout_constraint

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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/viewStopScheduleItem_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dp"
    android:clickable="true"
    android:focusable="true"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="0dp"
    app:cardElevation="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/viewStopScheduleItem_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <ImageView
            android:id="@+id/viewStopScheduleItem_barImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/status_bar_item_background"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp" />

        <ImageView
            android:id="@+id/viewStopScheduleItem_busImageView"
            android:layout_width="32dp"
            android:layout_height="32dp"
            app:srcCompat="@drawable/status_bar_item_background"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginBottom="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeShortNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintTop_toTopOf="parent" \\
            android:layout_marginTop="8dp"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginLeft="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent" \\
            android:layout_marginTop="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_stopTime1TextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:
            29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:
            29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1
            :29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:
            9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29
             PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29
             PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM
             1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM"
            android:textAlignment="viewStart"
            android:layout_gravity="start"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"/>

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

【讨论】:

    【解决方案2】:

    @TheHeuman, MohammadRexa Abiri 的回答将解决您的问题,但似乎您可以更多地清理您的 xml 文件。 请看下面的xml和附加的截图。

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView                  
      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/viewStopScheduleItem_cardView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginBottom="1dp"
      android:clickable="true"
      android:focusable="true"
      app:cardBackgroundColor="@android:color/white"
      app:cardCornerRadius="0dp"
      app:cardElevation="4dp">
    
      <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/viewStopScheduleItem_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
    
        <ImageView
          android:id="@+id/viewStopScheduleItem_barImageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:srcCompat="@android:drawable/status_bar_item_background" />
    
        <ImageView
          android:id="@+id/viewStopScheduleItem_busImageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          app:layout_constraintStart_toEndOf="@+id/viewStopScheduleItem_barImageView"
          app:layout_constraintTop_toTopOf="parent"
          tools:background="@android:color/black"
          tools:layout_height="48dp"
          tools:layout_width="48dp" />
    
        <TextView
          android:id="@+id/viewStopScheduleItem_routeShortNameTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_routeLongNameTextView"
          app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_routeLongNameTextView"
          tools:text="TextView" />
    
        <TextView
          android:id="@+id/viewStopScheduleItem_routeLongNameTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          app:layout_constraintStart_toEndOf="@+id/viewStopScheduleItem_busImageView"
          app:layout_constraintTop_toTopOf="parent"
          tools:text="TextView" />
    
        <TextView
          android:id="@+id/viewStopScheduleItem_stopTime1TextView"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          android:gravity="start"
          android:layout_marginTop="8dp"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toEndOf="@+id/viewStopScheduleItem_busImageView"
          app:layout_constraintTop_toBottomOf="@+id/viewStopScheduleItem_routeLongNameTextView"
          tools:text="1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM" />
    
      </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-11
      • 2017-08-21
      • 2012-03-19
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      相关资源
      最近更新 更多