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

inflateメソッドの最後のパラメーターとしてfalseを渡すことができます

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>

OK今私たちはスタイルで転がっています;)(申し訳ありませんが抵抗できませんでした)。次に、AndroidManifest.xml内でアクティビティを構成しましょう。

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

ループ内でOK:

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 に答える