【问题标题】:Lint warning for bad performance - 80+ Views性能不佳的 Lint 警告 - 80 多次查看
【发布时间】:2026-01-01 14:05:01
【问题描述】:

我收到此 lint 警告错误:highscores.xml has more than 80 views bad for performance。这是我的高分表,有 15 行和 3 列。这是我的应用程序的 Lite 版本,我的付费版本将容纳 30 行。

我的问题是如何优化这段代码?必须有更好的方法。

highscores.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="5dp"
    android:paddingBottom="65dp"
    android:background="@drawable/scroll" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="18sp"
        android:paddingBottom="10dp"
        android:paddingTop="65dp" />

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:paddingTop="5dp" >

        <Button 
            android:id="@+id/homeBtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="14sp" />

        <Button 
            android:id="@+id/clearHS"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="14sp" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:background="@drawable/scrollviewborder"
        android:layout_below="@id/title"
        android:layout_above="@id/linearlayout"
        android:fillViewport="true"
        android:scrollbars="none" >

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r1s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r1c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r1b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r2s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r2c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r2b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r3s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r3c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r3b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r4s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r4c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r4b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r5s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r5c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r5b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r6s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r6c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r6b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r7s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r7c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r7b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r8s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r8c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r8b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r9s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r9c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r9b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r10s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r10c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r10b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r11s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r11c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r11b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r12s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r12c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r12b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r13s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r13c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r13b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r14s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r14c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r14b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>

            <View 
                android:layout_width="fill_parent"
                android:layout_height="1dp"       
                android:background="#C2BEBF" />

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/r15s"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="25"
                    android:gravity="center"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/r15c"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="75"
                    android:gravity="center"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/r15b"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:textSize="14sp" />
            </TableRow>
        </TableLayout>
    </ScrollView>
</RelativeLayout>

【问题讨论】:

    标签: android xml performance lint


    【解决方案1】:

    ScrollView 中使用ListView 而不是TableLayout。这不仅允许您根据可用的屏幕大小仅拥有实际需要的行数,而且您可以摆脱按钮,根据单击列表项执行按钮应该执行的任何操作。

    【讨论】:

    • 我会调查ListView。每一行的按钮仍然需要在每一行上,因为它们对于每一行都是唯一的。话虽如此,ListView 仍然是要走的路,对吗?
    • @Matt:“每一行的按钮仍然需要在每一行上,因为它们对于每一行都是唯一的”——恕我直言,很少有应用程序具有普遍认为的良好设计。跨度>
    【解决方案2】:

    如果您非常想只创建TableLayout,那么您可以通过编程方式添加您的TableRow,而不是在您的layout.xml 中创建所有内容(这将避免Lint 警告并且您可以更改您的表格行根据您的意愿)。这是一个关于如何做到这一点的 sn-p。

    TableLayout table = new TableLayout(this);
    
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT);
    table.setLayoutParams(lp);
    table.setStretchAllColumns(true);
    
    TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            1.0f);
    TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            1.0f);
    for (int r = 0; r < 2; ++r)
    {
        TableRow row = new TableRow(this);
        for (int c = 0; c < 2; ++c)
        {
            Button btn = new Button(this);
            btn.setText("A");
            row.addView(btn, cellLp);
        }
        table.addView(row, rowLp);
    }
    setContentView(table);
    

    【讨论】:

      【解决方案3】:

      在您开始考虑应用的精简/付费版本之前,我是否可以建议您创建一些应用来学习所涉及的过程和语言?

      一个想要为他们的应用程序收费的开发人员正在询问如何使用 ListView,这一事实几乎让我想哭。这正是我不再从事开发工作的原因,因为许多没有经验的程序员提出了先赚钱,然后再学习如何编程的想法。

      我也很好奇你所有额外的 1dp 大小的视图元素。使用布局,您可以定义 paddingTop=1dp 或 marginTop=1dp 并删除整个元素。

      此外,您应该在使用 layout_weight 之前了解它的作用。当没有兄弟时,将每个布局设置为 layout_weight=1 是没有意义的。

      另外,在每个视图上使用重力=中心也不好。您可以在父布局中使用 layout_gravity 或重力标签来影响子布局的重力。

      看起来这个 XML 是由 WYSIWYG 生成的,对后端语言没有真正的了解。我们都从这里的提问中学习,但你必须问问自己,考虑到经验丰富的作者发布的免费软件数量,你会为初学者支付软件费用吗?

      【讨论】:

      • 初级医生在工作中学习的同时获得报酬。学徒汽车技工也是如此。但出于某种原因,软件开发人员应该免费工作并赠送他们的软件。不要买这个,珍惜你的时间,如果可以的话,总是收费。
      【解决方案4】:

      有一些方法可以避免这种情况:

      1.使用 RecyclerView

      如果您的设计有许多彼此相似的 UI 组件,那么您应该使用 RecyclerView,这是这种情况下的最佳方法,并在需要时处理视图的回收。

      2。使用复合 Drawable

      如果您的用户界面在TextViewEditTextButton 附近有图像,那么您可以使用@ 将图像添加到TextViewButtonEditText 的左侧、右侧、顶部、底部987654328@财产。您只需要使用复合可绘制对象,无需使用LinearLayoutImageView

      如果你想在左侧添加图像,你可以像这样使用drawable left:

      android:drawableLeft="@drawable/image"
      

      这将在TextViewButtonEditText 的左侧添加一个图像,具体取决于您使用的是哪个属性。

      3.使用约束布局

      这种强大的布局确实可以帮助您在这个单一布局中设计复杂的 UI,从而帮助您消除布局的嵌套。

      【讨论】: