IDEA GridLayout

IDEA GridLayoutIDEAGridLayout用xml就直接加就好详见https://blog.csdn.net/weixin_39251617/article/details/79711668但是用Java代码添加就比较麻烦代码:xml<?xmlversion="1.0"encoding="utf-8"?><RelativeLayout…

大家好,又见面了,我是你们的朋友全栈君。

IDEA GridLayout
用xml就直接加就好
详见

https://blog.csdn.net/weixin_39251617/article/details/79711668

但是用Java代码添加就比较麻烦
代码:
xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">

    <TextView  android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="just a text view!" android:background="@android:color/white" android:textSize="50sp" android:gravity="center" app:layout_constraintTop_toTopOf="parent"/>

    <GridLayout  android:id="@+id/g1" android:columnCount="4" android:rowCount="5" android:layout_below="@+id/t1" android:layout_width="match_parent" android:layout_height="wrap_content">
        <TextView  android:id="@+id/t2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="grid_text" android:background="@android:color/holo_green_light" android:textSize="50sp" android:layout_columnSpan="4" android:gravity="center" app:layout_constraintTop_toTopOf="parent"/>
    </GridLayout>

</RelativeLayout>

Java代码:

package com.example.ruchu.gridlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;

public class MainActivity extends AppCompatActivity {

    GridLayout gridLayout;

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

        gridLayout = findViewById(R.id.g1);

        for (int i = 0; i < 4 ; i++) { 
   
            for (int j = 0; j <4 ; j++) { 
   
                Button bn = new Button(this);
                bn.setText("lll");
                bn.setTextSize(40);
                bn.setBackgroundColor(0);
                bn.setPadding(bn.getLeft(),bn.getTop(),bn.getRight(),bn.getBottom());

                GridLayout.Spec rowSpec;
                rowSpec = GridLayout.spec(i+1);
                GridLayout.Spec columnSpec;
                columnSpec = GridLayout.spec(j);

                GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);

                params.setGravity(Gravity.FILL);

                gridLayout.addView(bn,params);
            }
        }
    }
}

效果图:
这里写图片描述

其中注意GridLayout的声明成MainActivity的成员,不能在成员函数内声明(我在这检查了半天),还有xml中第二个TextView的android:layout_columnSpan=”4”不能省略,不然下面的“LLL”就只有一列。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131275.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Maven配置及IDEA中配置Maven详解

    Maven配置及IDEA中配置Maven详解1、Maven安装配置首先到官网下载http://maven.apache.org/download.cgi 我们需要下载的bin.zip结尾的文件,下载后解压到指定文件夹(全英文路径) 配置环境变量确保JAVA环境变量已经配置好了。添加M2_HOME和MAVEN_HOME环境变量到Windows环境变量。 PATH变量添加%M2_HOME%\bin修…

    2022年5月11日
  • lookdiv激活码【2021最新】[通俗易懂]

    (lookdiv激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月26日
  • navcat 15 激活码[在线序列号]「建议收藏」

    navcat 15 激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
  • mac idea永久激活码【永久激活】

    (mac idea永久激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~M…

    2022年3月21日
  • goland2021激活码【2021免费激活】

    (goland2021激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~9ADCNKZL59-eyJsaWNlb…

    2022年3月22日
  • IDEA激活成功教程后一直提示JetbrainsAgent 相关的弹框问题

    IDEA激活成功教程后一直提示JetbrainsAgent 相关的弹框问题激活成功教程后打开IDEA就弹框,关闭之后会自动打开浏览器,隔一会也会弹出来 也是一样的问题一开始是说把txt 和 jar 文件放一个路径下之类的方法,几经波折,发现没任何用处~最后各种搜索排查,在设置下更改配置就不弹啦~settings设置下搜索agent 取消”Instrumenting agent(requires debugger restart)”在 Reload classes after compilation:选择第一个 Always…

    2022年8月20日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号