【发布时间】:2020-04-03 21:05:58
【问题描述】:
我的目标是在 FlexboxLayout(或类似的东西)中有效地显示大量 TextView(带背景)。
我想以一种“标签”布局样式显示有关电影中演员和工作人员的信息
How the layout looks like (one of the FlexboxLayouts is marked)
我通过 LayoutInflater动态 将 TextViews 膨胀到 FlexboxLayout。 问题当然是 大量数据(这意味着大量视图,有时在 20 多个 Flexbox-Layouts 中有 30 个或更多)我的片段的创建时间/activity 增加,activtiy 需要更长的时间才能打开。
我膨胀的 TextView:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tag_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="5dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:gravity="center_vertical"
android:background="@drawable/bg_tag"
android:textColor="?android:textColorSecondary"
android:text="@string/placeholder" />
我的容器布局(TextViews 膨胀到 FlexboxLayout @+id/fxl_names):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?attr/colorOnBackground"
android:padding="5dp"
android:textSize="16sp"
android:textStyle="bold"
android:text="test" />
//TextViews get inflated into this FlexboxLayout
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/fxl_names"
app:flexWrap="wrap"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view_more"
android:visibility="gone"
android:layout_margin="0dp"
android:text="Show all"
style="@style/Widget.MaterialComponents.Button.TextButton.Dialog" />
</LinearLayout>
感谢您提供有关优化此布局的任何帮助/建议!
【问题讨论】:
标签: android android-layout layout-inflater android-flexboxlayout layout-optimization