2011-10-29 18 views
0

私は初心者ですから、助けてください。私はStackOverflowで多くの記事を読んでいましたが、私はGoogleで疑問を捜しましたが、良い答えを見つけるのは難しいです。ここでFrameLayoutとTextViewを使用してカスタムImageButtonクラスを作成する

は私がしようとしているものです:

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1">  
    <ImageButton 
     android:background="@layout/roundcorners" 
     android:id="@+id/hug" 
     android:scaleType="centerInside" 
     android:cropToPadding="false" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingBottom="10dip" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:src="@drawable/hug"> 
    </ImageButton> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center" 
     android:textColor="#000000" 
     android:textSize="15dip" 
     android:textStyle="bold" 
     android:paddingBottom="5dip" 
     android:clickable="true" 
     android:text="Hug"> 
    </TextView> 
</FrameLayout> 

あなたの上に、みんなは私が必要なもののXMLバージョンを見ることができます。

ポイントは...実行時にこれらのFrameLayoutsの多くがあります。ボタンを記入するためのすべての情報は、データベースから来ます。

私はデータベースからすべてのレジスタを通してループを使用し、新しいFrameLayoutクラスをインスタンス化することができるJavaクラスを作成する必要があります(このクラスは上記のXMLからわかるようにImageButtonとTextViewを持たなければなりません)

for (int i = 0; i < mArray.length; i++) { 
     button = new MyNewImageButton(name, src, text); 
} 

上記は単純化するためのものです。つまり、作成しようとしているこのクラスのインスタンスを作成するときに、データベースからパラメータを渡すということです。もちろん、作成されたすべてのボタンがレイアウトに追加されます。

私の質問です:私はXMLを使ってこれを行う方法を知っていますが、私は本当にこれを行うクラスを作成するのに苦労しています。

どんな助けもありがとうございます。

P .:私が英語で間違いを犯すと申し訳ありません。私はブラジル人です。いつか私の英語は完璧です!申し訳ありませんが、この質問に既に回答があった場合。


申し訳ありませんが別の質問をするために私自身の質問にお答えください。コメントを使用しようとしましたが、文字数に制限がありますので、本当に申し訳ありません。

ちょっと男と@ブレセンム。まあ...私はインフレータを使用しようとし、私は次のコードを思いついた:

super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    // ******************************************* 
    setContentView(R.layout.main);  


    //this is my main screen 
    //it's a linearlayout vertical orientation 
    ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal); 

    //these two new LinearLayouts will be one above the other, 
    //just like two lines 
    LinearLayout l1 = new LinearLayout(this); 
    LinearLayout l2 = new LinearLayout(this); 

    //inside of each linearlayout I set the orientation to horizontal 
    //so, everytime a picture is inflated from xml, it will fill in one 
    //linearlayout 
    l1.setOrientation(LinearLayout.HORIZONTAL); 
    l2.setOrientation(LinearLayout.HORIZONTAL); 

    //setting linearlayout parameters, so they fill the whole screen 
    l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 


    //the first two inflated xml imagebuttons I add to LinearView1 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l1, true); 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l1, true); 

    //the next two inflated xml imagebuttons I add to LinearView2 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l2, true); 
    view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao, 
      l2, true); 

    //after the above, we should have a grid 2X2 


    //after linearlayouts are filled, I add them to the main screen 
    parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1)); 
    parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1)); 

しかし、これは動作していません。エラーログに次のメッセージが表示されます。

"未処理のイベントループ例外"。

私が間違っていることについてのアイデアはありますか?

ありがとうございます!

答えて

1

xmlからビューを作成してレイアウトに追加しようとしているだけの場合は、 LayoutInflaterを使用します。あなたは、フレームのレイアウトやビューを拡張するクラスを作成しようとしている場合アクティビティ内

FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
     R.id.YOUR_VIEW_XML,null); 
layout.addView(frame); 

のようなものを使用します。パラメータをとり、必要な値を割り当てるコンストラクタを作成します。

EDIT:あなたはそれらの要素にidを設定している場合、あなたは

TextView text = (TextView)frame.findViewById(R.id.yourtextview); 

してアクセスすることもできますし、それが聞こえる

TextView text = (TextView)frame.getChildAt(0); 
+0

こんにちはBlessenmは、はい、あなたの提案は、おかげで働きました。 *どのように私は膨らんだXMLの内部の各要素を参照するのですか?つまり、ImageButtonとTextViewへの参照が必要なので、データベースの値を設定することができます。提案はありますか? –

+0

iveは私の答えを更新しました – blessenm

0

のような子インデックスを使用することができます アセス要素インサイド へImageButtonになるビュークラスとFrameLayoutでラップされたTextViewを作成する方法を探しているようです。

この場合、独自のViewクラスを作成することができます。おそらく、FrameLayoutを拡張するViewクラスです。カスタムビューを作成する方法の詳細については、この開発者の記事を参照してください。 http://developer.android.com/guide/topics/ui/custom-components.html

具体的に "化合物のコントロール" セクション:http://developer.android.com/guide/topics/ui/custom-components.html#compound

関連する問題