草庐IT

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

coder 2023-12-22 原文

我正在创建一个 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/

有关android - 通过布局上的 80 多个 View 增强应用程序性能的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  7. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐