0

我想给一个项目充气一次并循环使用它。我目前有一个解决方案,但很可能有更好的方法。此外,除非有调用,否则程序不会运行view.removeView,这是有道理的,但如果我想catBtn稍后在应用程序中添加,这似乎很危险)。

现有代码:

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

现有的 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>
4

1 回答 1

1

您可以将 false 作为最后一个参数传递给 inflate 方法

LayoutInflator.from(context).inflate(res, parent, false);

这会导致膨胀的视图不附加任何内容。这样你就不必删除任何东西。这摆脱了 assets.removeView() 问题。但我认为这仍然可能是浪费。

看起来您只需要一些按钮:

<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">

让我们将其提取为一种样式:

<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>

好的,现在我们正在以风格滚动;)(抱歉无法抗拒)。现在让我们在 AndroidManifest.xml 中配置您的 Activity:

<activity android:name=".MyCatBtnActivity"
 ... Whatever else is in your activity
 android:theme="@style/Theme.WithAwesomeButtons"/>

现在在你的循环中确定:

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

我认为这就是你想要达到的目标。

于 2011-08-04T05:20:20.213 回答