Xml布局文件有太多的视图。我怎样才能减少它们?

时间:2016-10-23 14:47:04

标签: xml android-layout

我的xml布局文件有大约30个重复的LinearLayouts,就像代码片段中的那些一样。每个LinearLayout包含两个TextView和一个ImageView。问题是在android studio中我收到一条消息,说我有超过80个视图,并且你可以拥有的最大视图数是80。 所以我的问题是:如何减少我使用的观看次数?

`

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/LL1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="60dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@color/white">


                <TextView
                    android:id="@+id/TV1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="book"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="10dp"
                />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="책"
                    android:textSize="20sp"
                    android:layout_weight="1"
                    android:layout_gravity="center_vertical"
                    />

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@drawable/circle_gray_orange_ring"
                    android:src="@mipmap/audio_white_clear"
                    android:layout_margin="5dp"
                />
            </LinearLayout>
        <LinearLayout
            android:id="@+id/LL2"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@color/white">

            <TextView
                android:id="@+id/TV2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="expensive"
                android:layout_weight="1"
                android:textSize="20sp"
                android:layout_gravity="center_vertical"
                android:paddingLeft="10dp"
            />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="책"
                android:textSize="20sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/circle_gray_orange_ring"
                android:src="@mipmap/audio_white_clear"
                android:layout_margin="5dp"
            />
        </LinearLayout>`

1 个答案:

答案 0 :(得分:1)

  

我的布局文件有20-30个重复的线性布局,其中包含两个TextView和一个ImageView。

这是使用RecyclerView

的方法

你一定要从android开发者教程

中查看this guide

让您入门的提示,您重复LinearLayout可能是RecyclerView

中的一行

这会让你开始;)

相关问题