提问人:thejj 提问时间:10/30/2023 更新时间:10/30/2023 访问量:17
将视图动态添加到 scrollView 中的水平 linearLayout 会使它们更小
Adding Views dynamically to a horizontal linearLayout inside a scrollView makes them smaller
问:
我正在尝试将基于特定布局的 linearLayout 视图动态添加到位于 scrollView 中的父 linearLayout“cardContainer”。虽然添加了视图并且其内容很好,但由于某种原因,除第一张卡片外,每张卡片的宽度都略小。
父 scrollView 中的父 linearLayout
<ScrollView
android:id="@+id/svLibrary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etSearch"
android:fillViewport="true">
<LinearLayout
android:id="@+id/cardContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</ScrollView>
我尝试添加为视图的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:gravity="center">
<TextView
android:id="@+id/card_tvIndex"
style="@style/TextAppearance.AppCompat.Index"
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:text="14" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="7"
android:orientation="vertical">
<TextView
android:id="@+id/card_tvTitle"
style="@style/TextAppearance.AppCompat.CardTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingTop="5dp"
android:text="Title of a song" />
<TextView
android:id="@+id/card_tvArtist"
style="@style/TextAppearance.AppCompat.CardArtist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Artist Name" />
</LinearLayout>
<TextView
android:id="@+id/card_tvDuration"
style="@style/TextAppearance.AppCompat.Duration"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="3:21"
android:textAlignment="center" />
</LinearLayout>
Java 代码
void addInfoToCard() {
TextView index = layout.findViewById(R.id.card_tvIndex);
TextView title = layout.findViewById(R.id.card_tvTitle);
TextView artist = layout.findViewById(R.id.card_tvArtist);
TextView duration = layout.findViewById(R.id.card_tvDuration);
index.setText("" + (this.index+1));
title.setText(this.name);
artist.setText(this.artist);
duration.setText(AudioFile.formatDuration(this.length));
}
void displayCards(String filter) {
if (filter.isEmpty()) {
for (AudioCard card : list) {
if(card.layout==null) return;
cardContainer.addView(card.layout);
}
}
}
for (AudioCard card : list) {
card.layout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.audiocard, null);
card.addInfoToCard();
}
displayCards("");
答: 暂无答案
评论