0

ImageButtons を使用してグリッド ビューを動的に作成しようとしていますが、この問題があります。始める前に、私はAndroidとJavaの開発が初めてだと言わなければなりません(Objective-cの2.5年)。

Ok。だから私はこのコードを使用しています:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_background);

ArrayList<ImageButton> tmpStrRay = new ArrayList<ImageButton>();

Boolean load=true; 
for (int i= 0;load;i++){
    ImageButton iv = new ImageButton(this);
    InputStream ims;

    try {
        ims = getAssets().open("sm_backgrounds/bgSM_"+i+".jpg");
        Drawable d = Drawable.createFromStream(ims, null);
        iv.setImageDrawable(d);
        tmpStrRay.add(iv);

    } catch (IOException e) {
        load = false;
        e.printStackTrace();
    }

    System.out.print(i);
}

GridView grid = (GridView) findViewById(R.id.gridView);
ArrayAdapter<ImageButton> adapter = new ArrayAdapter<ImageButton>(this, android.R.layout.simple_list_item_1, tmpStrRay);
grid.setAdapter(adapter);

}

そして、私は私のレイアウトにこのXMLを持っています:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BackgroundSelector" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" 
        android:id="@+id/topLinear">

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="close"
            android:text="Close"
            android:textSize="15sp" />

        <Button
            android:id="@+id/Button01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="showCamera"
            android:text="Camera"
            android:textSize="15sp" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/topLinear" >

        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3" >
        </GridView>
    </ScrollView>

</RelativeLayout>

そして、テキスト付きのグリッドのこの奇妙な結果を取得しています。通常、ボタン自体を表示するのではなく、ImageButton を説明すると言います。ほとんどの人にとっては簡単だと思いますが、答えがある場合は、それについて説明したいと思います。

ありがとう !!!

4

1 に答える 1

0

android.R.layout.simple_list_item_1 レイアウト (TextView) を受け取り、この textView を tmpStrRay[i].toString() テキストで埋める ArrayAdapter アダプターを使用します。

配列表示に TextView 以外のもの (ImageView など) を使用したり、toString() の結果以外のデータをビューに表示したりするには、getView(int, View, ViewGroup) をオーバーライドして、必要なビューのタイプを返します。

于 2013-01-08T22:22:51.707 に答える