草庐IT

Android:在 gridlayout 或 gridview 中创建具有动态大小的网格

coder 2023-12-11 原文

我打算制作网格基础应用程序,其中网格的数量会根据用户的点击而改变。网格模式:- 2*2、4*4 ... [例如,用户将单击正确的网格,然后网格数量将增加]。我 cn 能够制作网格,但它不适合布局。那么,如何根据用户的点击更改网格的数量以及如何动态设置网格的大小。

这里我附上了我想要实现的图像。

当用户点击不同颜色的网格时,网格数量会增加,如第二张图片。 p>

图片:1

图片:2

用户点击后网格数量增加。

最佳答案

最后我得到了我的问题的答案:

这里需要将 SquareImageView 用于 imageview 并以编程方式设置 gridview 的列,现在看下面的代码:

SquareImageView.java

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to
                                                                    // width
}
}

为查看充气器创建自定义适配器,

layout.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" >
<com.example.SquareImageView
    android:id="@+id/image_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</com.example.SquareImageView>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

int counter = 1;

CustomGrid adapter;
static int colum = 2;

public static GridView gridview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adapter = new CustomGrid(MainActivity.this);
    gridview = (GridView) findViewById(R.id.gridview);

    gridview.setNumColumns(2);

    gridview.setAdapter(adapter);

}

public class CustomGrid extends BaseAdapter {
    private Context mContext;
    CustomGrid adgg;
    int mNumColumns = 2;
    private Random rand = new Random();
    GridView gg;
    int color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    float alfa = 0.1f;
    int rand_ind = rand.nextInt((2 * 2) - 0) + 0;

    public CustomGrid(Context c) {
        mContext = c;
        // this.Imageid = Imageid;
        // this.web = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mNumColumns * mNumColumns;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View gridView;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View grid = new View(mContext);
        grid = inflater.inflate(R.layout.layout, null);
        final View imageView = (ImageView) grid.findViewById(R.id.image_grid);

        if (convertView == null) {
            if (position == rand_ind) {
                imageView.setBackgroundColor(getColorWithAlpha(color, alfa));
            } else {
                imageView.setBackgroundColor(color);
            }
        } else {
            grid = (View) convertView;
        }

        grid.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                if (position == rand_ind) {

                    // Correct postion click time if condition occur..........


                    if (counter == 2) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 6) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.08f;
                    } else if (counter == 8) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 12) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 14) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 16) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.1f;
                    } else if (counter == 18) {
                        mNumColumns = mNumColumns + 1;
                        alfa = alfa + 0.05f;
                    } else if (counter >= 18) {
                        // mNumColumns = mNumColumns + 1;
                        alfa = 0.8f;

                    }

                    gridview.setNumColumns(mNumColumns);
                    gridview.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                    rand_ind = rand.nextInt((mNumColumns * mNumColumns) - 0) + 0;

                } else {

                    // Here code for nagative click............ whem click
                    // nagative at time geanarate this code.........
                }
            }
        });

        return grid;
    }

    public int getColorWithAlpha(int color, float ratio) {
        int newColor = 0;
        int alpha = Math.round(Color.alpha(color) * ratio);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        newColor = Color.argb(alpha, r, g, b);
        return newColor;
    }
}

@Override
public void onClick(View v) {
}
}

我希望这个答案对任何人都有帮助。

关于Android:在 gridlayout 或 gridview 中创建具有动态大小的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314550/

有关Android:在 gridlayout 或 gridview 中创建具有动态大小的网格的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  4. ruby-on-rails - Rails 3.1 中具有相同形式的多个模型? - 2

    我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#

  5. ruby - 如何在 Ruby 中创建无类 DSL? - 2

    我正在尝试找出如何为我的Ruby项目创建一种“无类DSL”,类似于在Cucumber步骤定义文件中定义步骤定义或在Sinatra应用程序中定义路由。例如,我想要一个文件,其中调用了我的所有DSL函数:#sample.rbwhen_string_matches/hello(.+)/do|name|call_another_method(name)end我认为用我的项目特有的一堆方法污染全局(内核)命名空间是一种不好的做法。因此方法when_string_matches和call_another_method将在我的库中定义,并且sample.rb文件将以某种方式在我的DSL方法的上下文中

  6. ruby-on-rails - 如何在 Rails 3 中创建自定义脚手架生成器? - 2

    有这些railscast。http://railscasts.com/episodes/218-making-generators-in-rails-3有了这个,你就会知道如何创建样式表和脚手架生成器。http://railscasts.com/episodes/216-generators-in-rails-3通过这个,您可以了解如何添加一些文件来修改脚手架View。我想把两者结合起来。我想创建一个生成器,它也可以创建脚手架View。有点像RyanBates漂亮的生成器或web_app_themegem(https://github.com/pilu/web-app-theme)。我

  7. ruby - 为什么在 ruby​​ 中创建 Rational 不需要新方法 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?

  8. HBase Region 简介和建议数量&大小 - 2

    Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile

  9. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  10. ruby - 在 Ruby 中创建按公共(public)键值分组的新哈希 - 2

    假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解

随机推荐