AndroidUi--用GridLayout实现计算器界面
2017-09-08 10:41
549 查看
这次是用GridLayout(网格布局器) 实现计算器界面,网格布局器是android4.0的布局。利用类似于组件的布局,可以设置组件的位置。
package com.example.luoge.uisample3; /*实现计算器界面*/ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.widget.Button; import android.widget.GridLayout; public class MainActivity extends AppCompatActivity { GridLayout gridLayout; String [] ops = new String[]{ "1" ,"2","3","/", "4","5","6","*", "7","8","9","-", ".","0","=","+", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridLayout = (GridLayout)findViewById(R.id.root); for(int i=0;i<ops.length;i++) { Button bn = new Button(this); //设置按钮内容 bn.setText(ops[i]); //设置文字大小 bn.setTextSize(40); bn.setBackgroundColor(0xffffbb33); bn.setPadding(10,10< 4000 /span>,10,10); 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"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" 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:backgroundTint="@android:color/holo_orange_light" android:text="清除" /> </GridLayout>
相关文章推荐
- Lance老师UI系列教程第三课->QQ登录注册界面的实现(android-2012最新版)
- Lance老师UI系列教程第一课->QQ设置界面的实现(android)
- Lance老师UI系列教程第一课->QQ设置界面的实现(android)
- Android UI开发第三十九篇——Tab界面实现汇总及比较
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- 【Android UI】使用RelativeLayout与TableLayout实现登录界面
- Android之UI学习篇十:使用TabHost实现微博客户端界面
- [Android][UI]模拟器启动界面动画效果的简单实现
- Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- UI复习-布局管理器GridLayout(计算器界面)
- android 程式开发:(十)绘制用户界面 —— 10.1通过纯编码实现UI
- Android UI开发第三十九篇——Tab界面实现汇总及比较
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- Android UI开发第三十九篇——Tab界面实现汇总及比较
- 从零开始学android<使用嵌套布局实现计算器界面.十七.>
- Android UI控件之CheckBox实现墨迹天气设置布局界面(漂亮)
- android handler实现界面UI的更新
- Android UI控件之CheckBox实现墨迹天气设置布局界面(漂亮)
- Lance老师UI系列教程第三课->QQ登录注册界面的实现(android-2012最新版)