【问题标题】:Unable to add ImageButtons to gridView with adapter<ImageButton> in android无法在android中使用适配器<ImageButton>将ImageButtons添加到gridView
【发布时间】:2013-01-08 22:10:11
【问题描述】:

我正在尝试使用 ImageButtons 动态创建网格视图,但我遇到了这个问题。 在开始之前,我必须说我是 android 和 java 开发的新手(2.5 年的 Objective-c)。

好的。所以我使用这个代码:

@覆盖 公共无效 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>

我得到了带有文本的 Grid 这个奇怪的结果,通常我会说描述 ImageButton 而不是显示按钮本身。 我相信这对你们大多数人来说很容易 - 但如果你有答案,我真的很想得到一个解释。

谢谢!!!

【问题讨论】:

    标签: android layout gridview imagebutton


    【解决方案1】:

    您使用 ArrayAdapter 适配器,它采用 android.R.layout.simple_list_item_1 布局(即 TextView)并用 tmpStrRay[i].toString() 文本填充此 textView。

    要使用 TextViews 以外的东西来显示数组,例如 ImageViews,或者要让 toString() 结果之外的一些数据填充视图,请覆盖 getView(int, View, ViewGroup) 以返回您的视图类型想要。

    【讨论】:

    • 谢谢,我已经搜索了更多内容,您的答案和我在互联网上获得的信息完成了任务。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多