Kotlin03.gridLayout计算机界面

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(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



    
    
    
    

 

你可能感兴趣的:(Kotlin)