问题
I am getting this lint warning error: highscores.xml has more than 80 views bad for performance
. This is my high scores table that has 15 rows and 3 columns. This is the Lite version of my app and my paid version is going to hold 30 rows.
My question is how can I optimize this code? There has to be a better way.
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>
回答1:
Use a ListView
instead of a TableLayout
inside of a ScrollView
. Not only will that allow you to only have as many rows as is really needed based upon available screen size, but you can get rid of the buttons, doing whatever it is the buttons are supposed to do based on clicking on list items.
回答2:
Might I suggest before you start thinking about lite/paid versions of your app, that you create a few apps to learn the process and language involved?
The fact that a developer who wants to charge money for their app, is asking on SO how to use a ListView, nearly makes me want to cry. This is EXACTLY the reason I no longer work in development as so many inexperienced programmers come up with the idea to make money first, then learn how to program second.
I am also curious about all your extra View elements of size 1dp. Using layouts you can define either paddingTop=1dp or marginTop=1dp and remove the entire element.
Also, you should learn what layout_weight does before using it. There is no point setting each layout to layout_weight=1 when it has no siblings.
Also, using gravity=center on every view is not good. You can use layout_gravity or the gravity tag in the parent layout, to influence the gravity of the children.
It looks like this XML was generated by a WYSIWYG, with no real understanding of the backend language. We all learn from asking on here, but you have to ask yourself, would you pay money for software from a beginner given the amount of free software that experienced authors release?
回答3:
If you're pretty much into creating a TableLayout
only, then you can add your TableRow
programmatically instead of creating everything in your layout.xml
(This will avoid Lint
warnings & you can alter your table rows as per your wish). Here is a snippet on how you can do it.
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);
回答4:
There are some ways to refrain from this situation:
1. Use RecyclerView
If your design have many UI component which are similar to each other then you should use recycler view, which is the best approach in this case and handle recycling of views whenever needed.
2. Use Compound Drawable
If your UI has image near text view or edit text view or button or edit text then you can add image to the left, right, top, bottom of textview or button or edittext using drawble property. You just need to use compound drawbale no need to use linear layout and image view.
If you want to add image on the left side then, you can use drawble left like this:
android:drawableLeft="@drawable/image"
this will add a image to the left side of textview or button or edit text depends in which you are using this property.
来源:https://stackoverflow.com/questions/15202900/lint-warning-for-bad-performance-80-views