糟糕表现的皮棉警告 - 80多个视图

时间:2013-03-04 13:40:25

标签: android xml performance lint

我收到此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>

4 个答案:

答案 0 :(得分:7)

ListView内使用TableLayout代替ScrollView。根据可用的屏幕尺寸,这不仅可以让你只有真正需要的行数,而且你可以摆脱按钮,根据点击列表项做按钮应该做的任何事情。

答案 1 :(得分:3)

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

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

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

此外,您应该在使用之前了解layout_weight的作用。没有兄弟姐妹时,没有必要将每个布局设置为layout_weight = 1。

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

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

答案 2 :(得分:1)

如果您只是在创建TableLayout,那么您可以通过编程方式添加TableRow,而不是在layout.xml中创建所有内容(这样可以避免Lint警告&amp;您可以根据自己的意愿改变表格行。以下是有关如何执行此操作的摘要。

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 :(得分:0)

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

1。使用RecyclerView

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

2。使用复合可绘制

如果您的UI在文本视图或编辑文本视图或按钮或编辑文本附近有图像,则可以使用drawble属性将图像添加到文本视图或按钮或edittext的左侧,右侧,顶部,底部。您只需要使用复合抽签,而无需使用线性布局和图像视图。

如果您想在左侧添加图像,则可以像这样使用左侧的drawble:

android:drawableLeft="@drawable/image"

这会将图像添加到textview或按钮的左侧,或者根据您使用此属性的位置来编辑文本。

相关问题