iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android制作漂亮自适布局键盘的方法
  • 751
分享到

Android制作漂亮自适布局键盘的方法

方法布局Android 2022-06-06 09:06:27 751人浏览 八月长安
摘要

最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思路。 这里最上面的titlebar

最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思路。

这里最上面的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>

  是不是很酷?

  图标什么的自己上网找吧,这里就不贴出来了。

Android制作漂亮自适布局键盘的方法就先给大家介绍到这里,后续还会持续更新相关知识,希望大家继续关注编程网网站,谢谢!

您可能感兴趣的文章:解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法Android 设置Edittext获取焦点并弹出软键盘Android 显示和隐藏软键盘的方法(手动)Android 软键盘弹出时把原来布局顶上去的解决方法5种方法完美解决android软键盘挡住输入框方法详解Android键盘显示与隐藏代码Android软键盘遮挡的四种完美解决方案Android实现弹出键盘的方法Android中监听软键盘显示状态实现代码Android物理键盘事件解析


--结束END--

本文标题: Android制作漂亮自适布局键盘的方法

本文链接: https://www.lsjlt.com/news/26167.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 自适应布局的处理方法
    本篇内容介绍了“自适应布局的处理方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!左边固定,右边自适应(圣杯布局的实现): 代码如下:<...
    99+
    2023-06-08
  • html5自适应页面布局的方法
    这篇文章主要讲解了“html5自适应页面布局的方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“html5自适应页面布局的方法”吧!   一、静态布局(S...
    99+
    2024-04-02
  • css3多兰自适应布局的方法
    这篇“css3多兰自适应布局的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“css3多...
    99+
    2024-04-02
  • html左中右自适应布局的方法
    本篇内容介绍了“html左中右自适应布局的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在最新的html标准中 有了个calc css表...
    99+
    2023-06-08
  • Android弹出软键盘时把布局顶上去的解决方法
    当 Android 软键盘弹出时,布局会被顶上去的问题,可以尝试以下解决方法: 使用 android:windowSoftInputMode 属性:在 AndroidManifest.xml 文件中设置 Activity 的 androi...
    99+
    2023-08-31
    android java android studio
  • Vue前端项目自适应布局的简单方法
    目录一、单位尺寸二、基于rem实现自适应布局附:html5页面 的rem 布局适配方法总结一、单位尺寸 px%vw、vh: 窗口em: 针对父元素的font-sizerem:&ldq...
    99+
    2024-04-02
  • 使用css制作自适应布局的常见注意事项有哪些
    这篇文章主要介绍使用css制作自适应布局的常见注意事项有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!制作自适应布局最常使用的解决方法就是在页面初始化时,利用js去获取宽高再计算...
    99+
    2024-04-02
  • Android开发实现popupWindow弹出窗口自定义布局与位置控制方法
    本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下:布局文件:主布局文件:activity_main:<?xml version="1.0" encodi...
    99+
    2023-05-30
    android popupwindow 弹出窗口
  • Android自定义View实现游戏摇杆键盘的方法示例
    前言本文主要给大家介绍的是关于Android自定义View实现游戏摇杆键盘的相关内容,为什么会有这篇文章呢?因为在之前的一个项目,操作方向的方式为上下左右,左上需要同时按住左键和右键的方式进行操作。如下图:近来需要升级项目,操作方式改为类似...
    99+
    2023-05-30
    android 自定义view 游戏摇杆
  • vuedirective全局自定义指令实现按钮级别权限控制的操作方法
    目录概念全局自定义指令局部自定义指令钩子函数全局自定义指令项目应用在日常项目中,通常会需要根据后台接口返回的数据,来判断当前用户的按钮操作权限。对于当前登录用户来说,只有在当前按钮有...
    99+
    2023-02-06
    vue directive按钮级别权限控制 vue directive全局自定义指令
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作