GridLayout 简单实例

本文介绍了一个使用Android的GridLayout创建简易计算器应用程序的过程。通过代码示例展示了如何动态地为GridLayout添加按钮,并设置它们的位置,实现了计算器的基本界面布局。



代码部分:

src  GridLayoutTest.java

package cn.com.gridlayouttest;


import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;

public class GridLayoutTest extends Activity {

	GridLayout gridLayout;
	String[] chars=new String[]
			{
				"7","8","9","/",
				"4","5","6","*",
				"1","2","3","-",
				".","0","=","+"
			};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		gridLayout=(GridLayout) findViewById(R.id.root);
		for(int i=0;i<chars.length;i++){
			Button bn=new Button(this);
			bn.setText(chars[i]);
			bn.setTextSize(40);
			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);
			gridLayout.addView(bn,params);
		}
	}

}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.com.gridlayouttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.com.gridlayouttest.GridLayoutTest"
            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>


布局文件  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">  
    <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"  
        />  
    <Button  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_columnSpan="4"  
        android:text="清除"  
        />  
 </GridLayout>  


                
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值