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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IDEA激活成功教程后一直提示JetbrainsAgent 相关的弹框问题

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

    2022年8月20日
    9
  • pycharm21.2.3激活码_最新在线免费激活「建议收藏」

    (pycharm21.2.3激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~EC…

    2022年3月31日
    712
  • pycharm2021年激活码【注册码】

    pycharm2021年激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    59
  • idea mac激活码【注册码】[通俗易懂]

    idea mac激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    44
  • datagrip激活码2021_最新在线免费激活

    (datagrip激活码2021)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    128
  • mac. navcat 激活码【2022最新】2022.02.19

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

    2022年4月1日
    37

发表回复

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

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