关于网格布局gridLayout,动态创建内容

重要属性:

android:rowCount(行数量)
android:columnCount (列数量)
android:layout_row (位于第几行)
android:layout_rowSpan(跨几行)
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>

  <EditText
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnSpan="4"
        android:layout_columnWeight="1"
        android:layout_rowWeight="2"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="C"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="+/-"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="%"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="*"/>
 
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="0"
        android:layout_columnSpan="2"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="."/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="="/>

</GridLayout>

也可以动态创建里边的button

先在xml文件中创建一个gridLayout,并设置 android:columnCount=“4” 每行显示多少个;

 <GridLayout
                    android:id="@+id/grid_result_string"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/grey_btn_background"
                    android:columnCount="10"
                    >

然后根据需求,创建好view之后就通过addView添加到GridLayout中

for(int i = 0;i < barcode.length() ;i ++)
      {
         String substring = barcode.substring(i, i + 1);

         TextView textView = new TextView(this);
         textView.setTextSize(21);
         textView.setText(substring);

         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(55,55);
         layoutParams.setMargins(3,3,3,3);
         textView.setPadding(1,1,1,1);
         textView.setLayoutParams(layoutParams);
         textView.setGravity(Gravity.CENTER);

         textView.setBackgroundColor(getResources().getColor(R.color.white));
         grid_result_string.addView(textView);
      }

我这里做的是十个普通的框框,动态生成的;
在这里插入图片描述

关于new 的控件,没办法直接设置margin的问题

需要用到linearLayout.LayoutParams中来设置,如上面代码;

1、如果这个控件实在XML中定义的

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textview.getLayoutParams();
lp.leftMargin = 0;
textview.setLayoutParams(lp);

2、如果这个控件是我们new出来的,就会会发现用上面的方法就会有空指针报错了。然后我们用另一种方法

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);//4个参数按顺序分别是左上右下
textview.setLayoutParams(layoutParams);

原文链接:https://codeleading.com/article/27194067916/

————————————————
版权声明:本文为CSDN博主「SlowIsFastLemon」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/SlowIsFastLemon/article/details/118059337

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值