zoukankan      html  css  js  c++  java
  • gridlaylout 简单布局

    package com.example.gridlayout;
    
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.Button;
    import android.widget.GridLayout;
    
    
    public class MainGrid extends Activity {
        GridLayout grd;
        String[] chas=new String[]
                {
                  "7","8","9","*"
                  ,"4","5","6","/"
                  ,"1","2","3","+"
                  ,"0",".","=","-"
                
                };
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main_grid);
            grd=(GridLayout)findViewById(R.id.root);
            for(int i=0;i<chas.length;i++)
            {
                Button bt=new Button(this);
                 bt.setText(chas[i]);
                 bt.setTextSize(40);
                 bt.setHeight(200);
                    GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 2);
                    // 指定该组件所在列
                    GridLayout.Spec columnSpec = GridLayout.spec(i % 4);
                    GridLayout.LayoutParams params = new GridLayout.LayoutParams(
                            rowSpec , columnSpec);
                    // 指定该组件占满父容器
                    params.setGravity(Gravity.FILL);        
                    grd.addView(bt , params);
    
            }
        }
    }
    <?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="4px"
        android:layout_marginRight="4px"
        android:padding="5px"
        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>
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.gridlayout"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="17"
            android:targetSdkVersion="21" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainGrid"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    英语口语——英语连读、发音、英文音标、语调、语气、节奏等等
    高中语法_句法篇——名词性从句、定语从句、状语从句、被动语态、虚拟语气、倒装句
    高中语法_时态篇——笔记 -五大基本句型,十大单词类型、常用时态。
    《数据结构》 第一章 笔记
    《计算机网络》 第三章 数据链路层
    《计算机网络》第二章 物理层笔记
    《计算机网络》谢希仁版--第一章习题
    我个人的c#入门总结【勿入】
    排序算法总结
    C#基础知识点总结
  • 原文地址:https://www.cnblogs.com/gwazy/p/4466585.html
Copyright © 2011-2022 走看看