我在CardView(CV)内的ConstraintLayout(CL)内有一个TextView。 CL是CV中唯一的元素,CL内部有几个不同的ImageViews和TextViews。
通过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的重力设置为开始和顶部。
那么,如何去除填充物?
我已经附上了设计外观的屏幕截图
长字符串导致填充
没有填充的短字符串