android GridView 使用

九宫格

1、创建一个页面

HomeActivity.xml
public class HomeActivity extends Activity {
    private GridView gv_home;
    private String[] mTitleStrs;
    private int[] mDrawableIds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initUI();

        initDate();
    }

    private void initDate() {
        mTitleStrs = new String[]{
                "手机防盗",
                "通信卫士",
                "软件管理",
                "进程管理",
                "流量统计",
                "手机杀毒",
                "缓存清理",
                "高级工具",
                "设置中心"
        };
        mDrawableIds = new int[]{
                R.drawable.sdlr01,
                R.drawable.sdlr02,
                R.drawable.sdlr03,
                R.drawable.sdlr04,
                R.drawable.sdlr05,
                R.drawable.sdlr06,
                R.drawable.sdlr07,
                R.drawable.sdlr08,
                R.drawable.sdlr09,
        };
        //九宫格控件设置数据适配器(等同于ListView数据适配器)
        gv_home.setAdapter(new MyAdapter());
        //注册九宫格单个条目点击事件
        gv_home.setOnItemClickListener(new OnItemClickListener() {
            //点中列表条目索引position
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                switch (position) {
                case 0:
                    //开启对话框
                    showDialog();

                    break;
                case 8:
                    Intent intent = new Intent(getApplicationContext(),SettingActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
                }

            }
        });
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // 条目总数
            return mTitleStrs.length;
        }

        @Override
        public Object getItem(int position) {
            //条目
            return mTitleStrs[position];
        }

        @Override
        public long getItemId(int position) {
            //条目id
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getApplicationContext(), R.layout.gridview_item, null);
            TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
            ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);

            tv_title.setText(mTitleStrs[position]);
            iv_icon.setBackgroundResource(mDrawableIds[position]);
            return view;
        }

    }

    private void initUI() {
        gv_home = (GridView)findViewById(R.id.gv_home);

    }
}
activity_home.xml
<?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:orientation="vertical" >

    <GridView 
        android:id="@+id/gv_home"
        android:numColumns="3"
        android:verticalSpacing="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </GridView>

</LinearLayout>
gridview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        android:contentDescription="@string/desc"/>

    <TextView
        android:id="@+id/tv_title"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mkbt" />

</LinearLayout>
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值