android - 通过布局上的 80 多个 View 增强应用程序性能

标签 android android-layout

我正在创建一个 android 应用程序,它有一个特定的屏幕(布局)超过(120 多个 View ),它会生成警告:

   "main.xml has more than 80 views, bad for performance"

我有很多 TextViewsEditTextsRadioButtons 组,组织在 26 个 TableRows 上。

我能做些什么来提高应用程序性能并消除警告吗?

来源: main.xml_capture

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout00"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow00"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right|top"
        android:gravity="right" >

        <Button
            android:id="@+id/id_but_val"
            android:layout_width="@dimen/button_width"
            android:layout_height="wrap_content"
            android:background="@drawable/button_colored"
            android:text="@string/but_valid"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/id_but_up"
            android:layout_width="@dimen/button_width"
            android:layout_height="wrap_content"
            android:background="@drawable/button_colored"
            android:onClick="goToTop"
            android:text="@string/but_ret_up"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/id_but_back"
            android:layout_width="@dimen/button_width"
            android:layout_height="wrap_content"
            android:background="@drawable/button_colored"
            android:text="@string/but_ret_back"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:isScrollContainer="true"
        android:overScrollMode="always"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbarStyle="outsideInset" >

        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical" >

            <TableRow
                android:id="@+id/TableRow01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_84_obstacles_acces" />

                <RadioGroup
                    android:id="@+id/RadioGroup01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/RadioButton01"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_oui" />

                    <RadioButton
                        android:id="@+id/RadioButton02"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_non" />
                </RadioGroup>
            </TableRow>

            <TableRow
                android:id="@+id/TableRow02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_85_limit_tonnage" />

                <EditText
                    android:id="@+id/EditText01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:ems="@string/editTextEms"
                    android:inputType="numberDecimal" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top"
                android:visibility="gone" >

                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_86_derog" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView04"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_87_limit_hauteur" />

                <EditText
                    android:id="@+id/EditText03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:ems="@string/editTextEms"
                    android:inputType="numberDecimal" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_88_limit_largeur" />

                <EditText
                    android:id="@+id/EditText04"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:ems="@string/editTextEms"
                    android:inputType="numberDecimal" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow06"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView06"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_89_trav_jour" />

                <RadioGroup
                    android:id="@+id/RadioGroup06"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/RadioButton11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_oui" />

                    <RadioButton
                        android:id="@+id/RadioButton12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_non" />
                </RadioGroup>
            </TableRow>

            <TableRow
                android:id="@+id/TableRow07"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView07"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_90_acces_intemperies" />

                <RadioGroup
                    android:id="@+id/RadioGroup07"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/RadioButton13"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_oui" />

                    <RadioButton
                        android:id="@+id/RadioButton14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_non" />
                </RadioGroup>
            </TableRow>

            <TableRow
                android:id="@+id/TableRow08"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/row_margin_top" >

                <TextView
                    android:id="@+id/TextView08"
                    android:layout_width="@dimen/label_width"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:text="@string/ecran3_91_acces_de_client" />

                <RadioGroup
                    android:id="@+id/RadioGroup08"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/view_left_margin"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/RadioButton15"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_oui" />

                    <RadioButton
                        android:id="@+id/RadioButton16"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/radio_value_non" />
                </RadioGroup>
            </TableRow>

            <LinearLayout
                android:id="@+id/LinearLayout02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TableRow
                    android:id="@+id/TableRow09"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/row_margin_top" >

                    <TextView
                        android:id="@+id/TextView09"
                        android:layout_width="@dimen/label_width"
                        android:layout_height="fill_parent"
                        android:layout_marginLeft="@dimen/view_left_margin"
                        android:text="@string/ecran3_92_contraintes_reservoir" />

                    <EditText
                        android:id="@+id/EditText05"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/view_left_margin"
                        android:ems="@string/editTextEms"
                        android:inputType="text" />
                </TableRow>

                <TableRow
                    android:id="@+id/TableRow10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/row_margin_top" >

                    <TextView
                        android:id="@+id/TextView10"
                        android:layout_width="@dimen/label_width"
                        android:layout_height="fill_parent"
                        android:layout_marginLeft="@dimen/view_left_margin"
                        android:text="@string/ecran3_93_dalle_pret" />

                    <RadioGroup
                        android:id="@+id/RadioGroup10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/view_left_margin"
                        android:orientation="horizontal" >

                        <RadioButton
                            android:id="@+id/RadioButton19"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/radio_value_oui" />

                        <RadioButton
                            android:id="@+id/RadioButton20"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/radio_value_non" />
                    </RadioGroup>
                </TableRow>
<!-- TableRow10 to TableRow26 -->
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

最佳答案

如果没有看到您的完整布局,我会说用 ListView 替换 TableLayout。 我猜你的每一行都有多个 Views,如果你切换到 ListView 你将避免将整个布局加载到内存中的需要,而是您将仅加载 ListView 屏幕上可见的行以及除此之外的任何 View 。

使用上述方法,您将能够从当前布局中删除大量行。

编辑:

根据您添加的布局,一些建议:

  • TableRow 可以用作布局中的普通 View ,但它的主要目的是作为 TableLayout 的子项。
  • 如果您不希望 tableRow00 中的 Buttons 具有相同的宽度,那么您可以进行小幅改进,将 LinearLayout00 替换为当前布局 使用 RelativeLayout,移除 tableRow00,然后使用规则放置 ButtonsScrollViewRelativeLayout
  • LinearLayout01 替换为 TableLayout
  • 我不知道你为什么添加 LinearLayout02(我猜你所有的行都是一样的?!)但你应该删除它并直接附加 TableRow09 - TableRow26LinearLayout01
  • 我的其余答案仍然存在。

关于android - 通过布局上的 80 多个 View 增强应用程序性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10313496/

相关文章:

java - 如何在相机中应用自定义滤镜 [Surfaceview Preview]...?

Android BLE onConnectionUpdated()

android - 单击菜单向右移动屏幕;像 Facebook

具有自定义属性的 Android selector.xml 抛出 XmlPullParserException

android - GridLayout 列宽

android - 回收站 View 项目中的文本超出屏幕范围

android - 在 1.x 模拟器中运行 Android 2.x 应用程序

带有 BroadcastReceiver 内部类的 Android AlarmManager

android - 如何更改 xml 中的开关输入文本颜色?

android - android :layout_gravity and android:gravity之间的区别