gpt4 book ai didi

android - 如何在 Android 中创建自定义 gridview(如矩阵结构)

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:11 27 4
gpt4 key购买 nike

我想创建一个像 gridview 这样的矩阵,比如 10x20 矩阵

我们要指定View的行数和列数即10x20

如果屏幕很低,它应该水平和垂直滚动例如下图描述了 Matrix Gridview每个单元格代表(0,0) (0,1) 等等...(1,0) (1,1) 等..

如何生成这种类型的 View ?提前致谢....!!!

enter image description here

最佳答案

试试这个:

GridView 自定义适配器

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

public class GridViewCustomAdapter extends BaseAdapter {

ArrayList<String> items;

static Activity mActivity;

private static LayoutInflater inflater = null;

public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
mActivity = activity;
items = tempTitle;

inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public final int getCount() {

return items.size();

}

@Override
public final Object getItem(int position) {
return items.get(position);
}

@Override
public final long getItemId(int position) {

return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

View v = null;

v = inflater.inflate(R.layout.item, null);

Button tv = (Button) v.findViewById(R.id.button);
tv.setText(items.get(position));

return v;
}

}

gridview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/white"
android:orientation="vertical" >

<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="20dip"
android:gravity="center"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:numColumns="20"
android:stretchMode="columnWidth" >
</GridView>

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:layout_width="80dip"
android:layout_height="80dip"
android:textSize="10sp"
android:background="@android:color/holo_blue_light"
android:textColor="@android:color/black"
android:textStyle="bold" />

GridViewActivity

公共(public)类 GridViewActivity 扩展 Activity {

private GridView list;
ArrayList<String> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
for (int i = 0; i < 10; i++) {
for(int j=0;j<20;j++)
data.add(i+"-"+j);
}
GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

list = (GridView) findViewById(R.id.grid_view);
list.setAdapter(adapter);

}

输出:

enter image description here

关于android - 如何在 Android 中创建自定义 gridview(如矩阵结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26501752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com