2013-03-04 50 views
0

我得到這個皮棉警告錯誤:highscores.xml has more than 80 views bad for performance。這是我的高分表,有15行3列。這是我的應用程序的Lite版本,我的付費版本將持有30行。不良表現警告 - 80+意見

我的問題是如何優化此代碼?一定有更好的方法。

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> 

回答

5

使用ListView而不是TableLayout一個ScrollView內。根據可用的屏幕大小,這不僅可以讓您只有真正需要的行數,但您可以擺脫按鈕,根據按鈕列表項目執行按鈕應該執行的操作。

+0

我會研究''''ListView''''。每行的按鈕仍然需要放在每一行上,因爲它們對於每行都是唯一的。有人說,'''''ListView''''仍然是要走的路線,對嗎? – Matt 2013-03-04 13:48:11

+1

@Matt:「每行的按鈕仍然需要放在每一行上,因爲它們對於每一行都是唯一的。」 - 恕我直言,通常認爲好的設計很少有這樣的應用程序。 – CommonsWare 2013-03-04 14:16:02

1

如果你幾乎成只創建一個TableLayout,那麼你可以通過編程添加TableRow,而不是在你的layout.xml創造一切(這將避免Lint警告&你可以改變你的錶行按你的願望)。這裏有一個關於如何做到這一點的片段。

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); 
1

我可以建議你開始思考你的應用程序的精簡版/付費版本,您創建一些應用程序來學習涉及的過程和語言之前?

事實上,一個想爲自己的應用程序收錢的開發人員詢問如何使用ListView,幾乎讓我想哭。這正是我不再從事開發工作的原因,因爲許多沒有經驗的程序員想出先賺錢的想法,然後再學習如何編程。

我也很好奇所有額外的視圖元素的大小1dp。使用佈局,您可以定義paddingTop = 1dp或marginTop = 1dp並刪除整個元素。

此外,您應該在使用之前瞭解layout_weight的功能。當沒有兄弟姐妹時,沒有必要將每個佈局設置爲layout_weight = 1。

另外,在每個視圖中使用gravity = center並不好。您可以在父佈局中使用layout_gravity或重力標籤來影響兒童的重力。

看起來這個XML是由所見即所得產生的,沒有真正理解後端語言。我們都從這裏詢問,但是你必須問自己,你是否會因爲經驗豐富的作者發佈免費軟件而從初學者那裏爲軟件付錢?