2011-08-04 8 views
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> 

答えて

1

に既存あなたが膨張原因インフレート法

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

の最後のパラメータとして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); 
} 

私は、これはあなたが達成しようとしているものの一種であると思います。

+0

あなたの例は、すぐには使えませんでしたが、良いスタートに私を得ました。私は残りの部分を(編集として)得たリンクを投稿しましたが、あなたの投稿が90%あったので、あなたは信用を得ると言います。それだけでなく、あなたの答えにリアルタイムを入れて、あなたが行った長さに本当に感謝します。それは確かに+1です。ありがとう! – Jacksonkr

+0

申し訳ありませんが、私はそれをコンパイルしませんでした。あなたが残りの道を行くとうれしい:) –

関連する問題