【问题标题】:How to add imagebuttons to gridview in android如何在android中将图像按钮添加到gridview
【发布时间】:2012-10-04 19:58:42
【问题描述】:

我不知道如何将图像按钮添加到我的网格视图。我必须为每个图像按钮执行单独的代码,因为它们做的事情完全不同。我该怎么做?

我最终改用了表格布局

代码:

package com.mysoftware.mysoftwareos.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Import views

        //Setup onClickListener for the buttons

        //Setup GridView
    }

    @Override
    public boolean onKeyDown(int KeyCode, KeyEvent event) {
        if ((KeyCode == KeyEvent.KEYCODE_BACK)) {
        //Do nothing to prevent the back button to kill the launcher
        return true;
        }
    return super.onKeyDown(KeyCode, event);
    }

    public void onClick(View src) {
        switch(src.getId()) {
        }
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF8090A0" >

<GridView
    android:id="@+id/appsGridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:numColumns="3" >
</GridView>

</RelativeLayout>

【问题讨论】:

  • 发布您用来显示网格视图的代码。
  • 我真的没有那么多代码,但我已经上传给你看
  • gridviews 需要某种适配器。如果您没有,那将是第一步。
  • 我在您的 XML 布局中看到了一个空的 GridView。为了向其中添加按钮,您尝试了什么?
  • 我最终改用了表格布局,感谢您的想法,非常感谢您的回答

标签: java android gridview imagebutton


【解决方案1】:

gridview 有一个您可以使用的 onitemclickedlistener,但如果按钮应该执行不同的操作,并且您不是从某种数据存储加载,那么您可能无论如何都不需要 gridview。几年前查看 Roman Nurik 的 DashboardLayout。您可以在 XML 或代码中将 ImageButtons 添加到它。如果您添加代码,您可以为每个按钮分配一个匿名的 onClickListener。如果您在 XML 中执行此操作,则可以将 onClick 属性设置为活动中的方法。

【讨论】:

  • 我最终改用了表格布局,感谢您的想法,非常感谢您的回答
  • 行得通。我喜欢仪表板布局,因为它会自动将子级布局在均匀间隔的网格中。它有一些错误,但效果很好 - 最多 6 个按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多