如何在Android中创建自定义gridview(像矩阵结构) [英] How to create custom gridview (Like matrix structure ) in Android

查看:139
本文介绍了如何在Android中创建自定义gridview(像矩阵结构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a matrix like gridview say 10x20 matrix

我们要指定视图的行数和列数,即10x20

we want to specify the number of rows and column of the View ie 10x20

如果屏幕较低,则应水平和垂直滚动 例如,下面的图像描述了Matrix Gridview 每个单元代表 (0,0)(0,1)等... (1,0)(1,1)等.

if the screen is low it should scroll to horizontally and vertically for example the image below describe the Matrix Gridview Each cell represent (0,0) (0,1) etc... (1,0) (1,1) etc..

如何生成这种类型的视图? 提前谢谢.... !!!

How can generate this type of view? Advance Thanks ....!!!

推荐答案

这可以通过GridLayout和动态单元格创建

This can be achieved by GridLayout and dynamic Cell creation

/* * *版权所有2012 Jess Anders * *根据Apache许可2.0版(许可")获得许可; *未经许可,您不得使用此文件. *您可以在以下位置获得许可的副本: * * http://www.apache.org/licenses/LICENSE-2.0 * *除非适用法律要求或书面同意,否则软件 *根据许可协议分发的内容是按原样"分发的, *没有任何明示或暗示的保证或条件. *有关特定语言的管理权限,请参阅许可证. *许可中的限制. */

/* * * Copyright 2012 Jess Anders * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    GridLayout gl;
    TextView[] text;
    int item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        gl = new GridLayout(MainActivity.this);
        gl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        gl.setOrientation(0);
        gl.setColumnCount(11);
        gl.setRowCount(3);

        text = new TextView[100];
        ScrollView sv = new ScrollView(this);
        sv.setScrollbarFadingEnabled(false);
        HorizontalScrollView scrolview = new HorizontalScrollView(this);
        scrolview.setScrollbarFadingEnabled(false);
        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(sv);
        sv.addView(scrolview);
        scrolview.setHorizontalScrollBarEnabled(true);
        setContentView(linearLayout);
        for (int i = 0; i < 100; i++) {
            for (int j = 0; i < 10; j++) {
                text[i] = new TextView(MainActivity.this);
                text[i].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                text[i].setText(String.valueOf(i) + "," + String.valueOf(j));
                text[i].setTextSize(25);
                text[i].setPadding(50, 25, 10, 25);
                text[i].setOnClickListener(new View.OnClickListener() {

                    int pos = item;

                    public void onClick(View v) {

                        Toast.makeText(getBaseContext(), pos + " Clicked",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                gl.addView(text[i]);
            }
        }

        scrolview.addView(gl);

    }

 }

这不是直接的解决方案,但是...我想我需要自定义当前不知道的GridView

This is not the straight solution but... i think i need to customize the GridView which i do`nt know currently

这篇关于如何在Android中创建自定义gridview(像矩阵结构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆