欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Kotlin03.gridLayout计算机界面

程序员文章站 2022-06-24 20:09:36
MainActivity.ktpackage com.example.gridlayoutimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Gravityimport android.widget.Buttonimport android.widget.GridLayoutclass MainActivity : AppCompatActivity() {...

MainActivity.kt

package com.example.gridlayout

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import android.widget.GridLayout

class MainActivity : AppCompatActivity() {

    private var chars = arrayOf("7", "8", "9", "÷",
            "4", "5", "6", "×",
            "1", "2", "3", "-",
            ".", "0", "=", "+")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var gridLayout = findViewById<GridLayout>(R.id.root)
        for (i in chars.indices) {
            val bn = Button(this)
            bn.text = chars[i]
            bn.textSize = 40F
            bn.setPadding(5, 35, 5, 35)
            val rowSpec = GridLayout.spec(i / 4 + 2)
            val columSpace = GridLayout.spec(i % 4)
            val  params = GridLayout.LayoutParams(rowSpec, columSpace)
            params.setGravity(Gravity.FILL)
            gridLayout.addView(bn, params)
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8" ?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4"
    android:id="@+id/root">
    <!-- 定义一个横跨4列的文本框,
    并设置该文本框的前景色、背景色等属性  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:textSize="50sp"
        android:layout_marginLeft="2pt"
        android:layout_marginRight="2pt"
        android:padding="3pt"
        android:layout_gravity="right"
        android:background="#eee"
        android:textColor="#000"
        android:text="0"/>
    <!-- 定义一个横跨4列的按钮 -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"/>
</GridLayout>

 

本文地址:https://blog.csdn.net/augfun/article/details/107882131

相关标签: Kotlin