单次充气的风格要多次使用

时间:2011-08-04 04:48:01

标签: android android-layout android-styles

我想夸大一个项目并在循环中使用它。我目前有一个解决方案,但最有可能是更好的方法。此外,除非有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>

1 个答案:

答案 0 :(得分: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);
}

我认为这是你想要实现的目标。