gpt4 book ai didi

ListView 中的 Android 约束布局 - 最后一项样式已损坏

转载 作者:行者123 更新时间:2023-11-30 05:00:00 33 4
gpt4 key购买 nike

我不明白为什么 ListView 的最后一项有点“错误”。为什么我在那里不见了,这是因为 ListView 没有满足底部的父级?我不这么认为,因为我在我的项目中设置了一个 minHeight。

我还尝试在底部设置一个填充(正如提到的另一个堆栈答案),但它没有成功。

首先,让我们看一下图形结果:

enter image description here

这是我的 Activity :

<?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"
tools:context=".Activity.SelectSequenceStartPractise">

<fragment android:name="fr.izio.enhancedtabata.Fragment.MainHeader" android:id="@+id/main_header" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" android:layout_height="wrap_content" />

<com.google.android.material.textview.MaterialTextView
android:layout_marginTop="12sp"
app:layout_constraintTop_toBottomOf="@id/main_header"
android:id="@+id/select_a_sequence_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select a sequence"
android:fontFamily="@font/exo2"
android:textColor="@color/design_default_color_primary"
android:textSize="32sp"
android:textAlignment="center"/>

<ListView
android:id="@+id/sequence_selection_container_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/select_a_sequence_text" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的元素(用于适配器):

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.CardView"
android:backgroundTint="@color/material_on_primary_emphasis_high_type"
android:minHeight="72dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="72dp"
android:padding="8dp">

<TextView
android:id="@+id/sequence_name_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="@font/exo2"
android:text="My super sequence"
android:textColor="@color/design_default_color_primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/sequence_time_duration"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="@font/exo2"
android:textColor="@color/design_default_color_secondary"
android:text="Duration: 50 s"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<TextView
android:id="@+id/sequence_pause_time_duration"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="6dp"
android:fontFamily="@font/exo2"
android:textColor="@color/design_default_color_secondary"
android:text="Pause: 10 s"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>

<TextView
android:id="@+id/sequence_contains_cycles_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="@font/exo2"
android:textColor="@color/design_default_color_secondary"
android:text="Contains 5 cycles"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

最后,这是来自 Adapter 的 getView 方法:

    @NotNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Sequence sequence = super.getItem(position);

if (convertView == null) {
// Let's inflate the view
convertView = LayoutInflater.from(getContext()).inflate(R.layout.sequence_item_selection_start_practice, parent, false);
}

TextView sequenceName = convertView.findViewById(R.id.sequence_name_text);
TextView sequenceDuration = convertView.findViewById(R.id.sequence_time_duration);
TextView sequencePauseDuration = convertView.findViewById(R.id.sequence_pause_time_duration);

if (sequence != null) {
sequenceName.setText(sequence.name);
sequenceDuration.setText("Cycle duration: " + String.valueOf(sequence.sequenceDuration) + " s.");
sequencePauseDuration.setText("Pause duration: " + String.valueOf(sequence.sequencePauseDuration) + " s.");
}

return convertView;
}

为什么 ListView 的最后一项不遵守我在该项中输入的约束?

最佳答案

当您将 subview 与父 View 的顶部和底部对齐时,“android:layout_height”值应为“wrap_content”或“0dp”。

app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"

为 subview 设置“minHeight”

关于ListView 中的 Android 约束布局 - 最后一项样式已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58414730/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com