Single inflation of a style to be used multiple times
I want to inflate an item once and use it in a loop. I currently have a solution, but there is most likely a better way. Also, the program won't run unless there is view.removeVie开发者_开发技巧w
call, which makes sense, but seems hazardous if I ever want to add catBtn
later in the app).
Existing code:
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
for(int i = 0; i < 10; ++i) {
LinearLayout assets = (LinearLayout)this.getLayoutInflater().inflate(R.layout.assets, null);
Button btn = (Button)assets.findViewById(R.id.catBtn);//new Button(this);
assets.removeView(btn);
col1.addView(btn);
}
Existing layout.assets
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/assets">
<ImageView android:focusable="true"
android:id="@+id/thumb"
android:background="@drawable/selectable"
android:layout_marginBottom="20dip"
android:src="@drawable/icon" android:layout_height="140dip" android:layout_width="250dip"/>
<Button android:id="@+id/catBtn"
android:layout_height="wrap_content"
android:background="@drawable/selectable"
android:text="Cat Button"
android:layout_width="120dip"
android:textSize="16dip"></Button>
</LinearLayout>
You could pass false as the last parameter to the inflate method
LayoutInflator.from(context).inflate(res, parent, false);
Which causes the inflated view to be attached to nothing. That way you don't have to remove anything. That gets rid of the assets.removeView() issue. But I think this still might be wasteful.
It looks like you just want some buttons:
<Button android:id="@+id/catBtn"
android:layout_height="wrap_content"
android:background="@drawable/selectable"
android:text="Cat Button"
android:layout_width="120dip"
android:textSize="16dip">
Let's extract that to a style:
<resources>
<declare-stylable android:name="awesome_button">
<attr android:name="awesomeButtonStyle" android:type="reference"/>
</declare-stylable>
<style android:name="AwesomeButton">
<item android:name="android:layout_height">wrap_content</item>
<item android:name="android:background">@drawable/selectable</item>
<item android:name="android:layout_width">120dp</item>
<item android:name="android:text">Cat Button</item>
<item android:name="android:textSize">16sp</item>
</style>
<style android:name="Theme.WithAwesomeButtons" parent="@android:style/Theme">
<item android:name="awesomeButtonStyle">@style/AwesomeButton</item>
</style>
<resources>
OK now we are rolling with style ;) (sorry couldn't resist). Now let's configure your Activity, inside the AndroidManifest.xml:
<activity android:name=".MyCatBtnActivity"
... Whatever else is in your activity
android:theme="@style/Theme.WithAwesomeButtons"/>
OK now within your loop:
for (int i=0; i<10; i++) {
// Let's get rid of the LayoutInflator (unless you want to use an xml layout
// in which case, make awesomeButton.xml and have it just have a button in it
// with attribute style="?awesomeButtonStyle").
Button button = new Button(this, null, R.attr.awesome_button.awesomeButtonStyle));
// Let's tag them with the integer counter so we can id them later
// You can set id, but there is a slight chance it will not be unique
// within the hierarchy. Later on you can either use col1.getChildView(index) to scan
// and look for these tags (or store them in a local array if col1 holds a lot of views)
// Then you can also evaluate the tag whenever you are referring to a button from
// within an OnClickListener or any View listener for that matter.
button.setTag(Integer.valueOf(i));
col1.add(button);
}
I think this is sort of what you are trying to achieve.
精彩评论