Android如何制作漂亮的自适布局的键盘
最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思路。
这里最上面的titlebar高度固定,下面输入的金额高度也固定(当然也可以自适应),主要是中间的数字键盘,高度和宽度需要自适应。先来张效果图:

最常见的解决方案是用线性布局,自适应当然是按比例,但布局中无%的概念,那就要用到layout_weight了,该属性的作用是决定控件在其父布局中的显示权重(具体概念就不多说了)。
这里用一个LinearLayout 将数字键盘与下面的支付类型进行包装,然后用一个大LinearLayout包住所有的数字键盘如下图,它与下面支付类型比例是6:1,这样数字键盘就会按屏幕大小高度与宽度进行变化,每一行数字键盘用一个LinearLayout,里面包3个数字显示Button按钮。

设置每行的LinearLayout的layout_height=0dp,layout_weight=1,具体设置如下:
<style name="layout_input_amount_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_marginBottom">1dp</item>
<item name="android:gravity">center</item>
<item name="android:orientation">horizontal</item>
</style>
这样就保证了上下自适应布局。然后行里面的Button也是平均分配,不过这里是横向布局:layout_width=0dp,layout_weight=1,具体设置如下:
<style name="btn_input_amount_style">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">40sp</item>
<item name="android:textColor">#333333</item>
<item name="android:background">@color/white</item>
</style>
这样就达到了上面的数字键盘的上下左右自适应了。现在的问题是其中的灰色边框怎么出来呢?TextView中没有设置border的属性,网上找的方法又很麻烦。
这里需要用到一个技巧,利用灰色背景,让两个按键间的margin=1,上下也是margin=1,但是最右边的3、6、9和最后一行不用加。
<Button
android:id="@+id/btn_1"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="1" />
为什么要设置layout_width=0dp?结合layout_weight,可以使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。如果设置成wrap_content,内容过长会导致上下无法对齐的情况。
下面为整个布局内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.view.InputAmountActivity"> <RelativeLayout
android:id="@+id/bar_input"
android:layout_width="match_parent"
android:layout_height="@dimen/title_bar_height"
android:background="@color/logo_background"
android:orientation="horizontal"> <TextView
android:id="@+id/bar_back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/transparent"
android:drawableLeft="@drawable/btn_back_detail"
android:paddingLeft="17dip"
android:paddingRight="17dip" /> <TextView
android:id="@+id/bar_title"
style="@style/title_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:singleLine="true"
android:text="输入金额" />
</RelativeLayout> <LinearLayout
android:id="@+id/txt_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bar_input"
android:background="@color/logo_background"> <TextView
android:id="@+id/txt_pay_amount"
android:layout_width="match_parent"
android:layout_height="115dp"
android:background="@color/transparent"
android:gravity="right|center"
android:paddingLeft="17dip"
android:paddingRight="20dp"
android:text="¥998"
android:textColor="@color/white"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/txt_amount"
android:orientation="vertical"> <LinearLayout
android:id="@+id/table_num"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#c8cbcc"
android:orientation="vertical"> <LinearLayout style="@style/layout_input_amount_style"> <Button
android:id="@+id/btn_1"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="1" /> <Button
android:id="@+id/btn_2"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="2" /> <Button
android:id="@+id/btn_3"
style="@style/btn_input_amount_style"
android:text="3" />
</LinearLayout> <LinearLayout style="@style/layout_input_amount_style"> <Button
android:id="@+id/btn_4"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="4" /> <Button
android:id="@+id/btn_5"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="5" /> <Button
android:id="@+id/btn_6"
style="@style/btn_input_amount_style"
android:text="6" />
</LinearLayout> <LinearLayout style="@style/layout_input_amount_style"> <Button
android:id="@+id/btn_7"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="7" /> <Button
android:id="@+id/btn_8"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="8" /> <Button
android:id="@+id/btn_9"
style="@style/btn_input_amount_style"
android:text="9" />
</LinearLayout> <LinearLayout style="@style/layout_input_amount_style"> <Button
android:id="@+id/btn_t"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="." /> <Button
android:id="@+id/btn_0"
style="@style/btn_input_amount_style"
android:layout_marginRight="1dp"
android:text="0" /> <ImageButton
android:id="@+id/btn_d"
style="@style/btn_input_amount_style"
android:src="@drawable/ico_del" /> </LinearLayout>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_weight="1"
android:orientation="horizontal"> <LinearLayout
android:id="@+id/layout_zhifubao"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/logo_background"
android:gravity="center"
android:orientation="vertical"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:src="@drawable/ico_zhifubao" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:text="支付宝"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout> <LinearLayout
android:id="@+id/layout_wechat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3cb034"
android:gravity="center"
android:orientation="vertical"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:src="@drawable/ico_wechat" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:text="微信"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout> <LinearLayout
android:id="@+id/layout_pay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff7700"
android:gravity="center"
android:orientation="vertical"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:src="@drawable/ico_pay" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:text="储值"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout> </LinearLayout> </RelativeLayout>
是不是很酷?
图标什么的自己上网找吧,这里就不贴出来了。
微信支付的源码和支付碰到的部分问题解决方法已上传至微信公众号【一个码农的日常】,其它知识可回复:数据库,NET ,JS 即可自行下载,以后会定期更新内容。
Android如何制作漂亮的自适布局的键盘的更多相关文章
- Android中制作自定义dialog对话框的实例
http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...
- Android模板制作
本文详细介绍模板相关的知识和如何制作Android模版及使用,便于较少不必要的重复性工作.比如我在工作中如果要创建一个新的模块,就不要需要创建MVP相关的几个类:Model.View.Presente ...
- css制作漂亮彩带导航条菜单
点击这里查看效果:http://keleyi.com/keleyi/phtml/divcss/17.htm 效果图: 以下是源代码: <!DOCTYPE html PUBLIC "-/ ...
- Android的学习第六章(布局一LinearLayout)
今天我们来说一下Android五大布局-LinearLayout布局(线性布局) 含义:线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 主要作用:主要对整个界面 ...
- 使用 CSS3 & jQuery 制作漂亮的书签动画
今天的教程是关于创建使用 CSS 旋转变换和 JavaScript 制作动画书签效果.我们的想法是展现出样书状结构,使单一的色板或列表点击切换.当点击其中一项,我们就会旋转以显示所选择的项目. 在线演 ...
- Android开发之详解五大布局
http://bbs.chinaunix.net/thread-3654213-1-1.html 为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是: LinearLayo ...
- CSS3制作漂亮的照片墙
CSS3可以做动画大家肯定都是耳熟能详的了,但是大家有木有巧妙的利用这一个功能来制作一款漂亮的照片墙呢? 那么今天我们就利用CSS3动画这一特性来一起制作漂亮的照片墙吧! 第一部分:HTML 这里我们 ...
- Android中常用的5大布局详述
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面. 所有的布局方式都可以归类为ViewGroup的 ...
- Android 开发之旅:深入分析布局文件&又是“Hello World!”
http://www.cnblogs.com/skynet/archive/2010/05/20/1740277.html 引言 上篇可以说是一个分水岭,它标志着我们从Android应用程序理论进入实 ...
随机推荐
- C# 在腾讯的发展
本文首发我的微信公众号"dotnet跨平台", 内容得到大家热烈的欢迎,全文重新发布在博客,欢迎转载,请注明出处. .NET 主要的开发语言是 C# , .NET 平台泛指遵循EC ...
- 【.net 深呼吸】细说CodeDom(1):结构大观
CodeDom 是啥东东?Html Dom听过吧,XML Dom听过吧.DOM一般可翻译为 文档对象模型,那 Code + DOM呢,自然是指代码文档模型了.如果你从来没接触过 CodeDom,你大概 ...
- SignalR系列续集[系列8:SignalR的性能监测与服务器的负载测试]
目录 SignalR系列目录 前言 也是好久没写博客了,近期确实很忙,嗯..几个项目..头要炸..今天忙里偷闲.继续我们的小系列.. 先谢谢大家的支持.. 我们来聊聊SignalR的性能监测与服务器的 ...
- SSH实战 · 唯唯乐购项目(中)
用户模块 三:一级分类的查询 创建一级分类表并导入基本数据 CREATE TABLE `category` ( `cid` int(11) NOT NULL AUTO_INCREMENT, ` ...
- C# DateTime日期格式化
在C#中DateTime是一个包含日期.时间的类型,此类型通过ToString()转换为字符串时,可根据传入给Tostring()的参数转换为多种字符串格式. 目录 1. 分类 2. 制式类型 3. ...
- MVC5+EF6+MYSQl,使用codeFirst的数据迁移
之前本人在用MVC4+EF5+MYSQL搭建自己的博客.地址:www.seesharply.com;遇到一个问题,就是采用ef的codefirst模式来编写程序,我们一般会在程序开发初期直接在glob ...
- 在Ubuntu 16.10 安装 git 并上传代码至 git.oschina.net
1. 注册一个账号和创建项目 先在git.oschina.net上注册一个账号和新建一个project ,如project name 是"myTest". 2.安装git sudo ...
- Cocos2d Android 环境搭建
1.在开始之前,需要先准备好资源如下,如果安卓开发环境有了直接装第3.4. 1.JDK 点击下载 (1.6) 2.ADT(已经自带Android SDK)点击下载 3.NDK 点击下载 4. ...
- C++中的命名空间
一,命名空间(namespace)的基本概念以及由来 1.什么是标识符: 在C++中,标识符可以是基本的变量,类,对象,结构体,函数,枚举,宏等. 2.什么是命名空间: 所谓的命名空间是指标识符的可见 ...
- AI人工智能系列随笔
初探 AI人工智能系列随笔:syntaxnet 初探(1)