Android制作漂亮自適布局鍵盤(pán)的方法
最近做了個(gè)自定義鍵盤(pán),但面對(duì)不同分辨率的機(jī)型其中數(shù)字鍵盤(pán)不能根據(jù)界面大小自已鋪滿(mǎn),但又不能每種機(jī)型都做一套吧,所以要做成自適應(yīng),那這里主講思路。
這里最上面的titlebar高度固定,下面輸入的金額高度也固定(當(dāng)然也可以自適應(yīng)),主要是中間的數(shù)字鍵盤(pán),高度和寬度需要自適應(yīng)。先來(lái)張效果圖:

最常見(jiàn)的解決方案是用線(xiàn)性布局,自適應(yīng)當(dāng)然是按比例,但布局中無(wú)%的概念,那就要用到layout_weight了,該屬性的作用是決定控件在其父布局中的顯示權(quán)重(具體概念就不多說(shuō)了)。
這里用一個(gè)LinearLayout 將數(shù)字鍵盤(pán)與下面的支付類(lèi)型進(jìn)行包裝,然后用一個(gè)大LinearLayout包住所有的數(shù)字鍵盤(pán)如下圖,它與下面支付類(lèi)型比例是6:1,這樣數(shù)字鍵盤(pán)就會(huì)按屏幕大小高度與寬度進(jìn)行變化,每一行數(shù)字鍵盤(pán)用一個(gè)LinearLayout,里面包3個(gè)數(shù)字顯示Button按鈕。

設(shè)置每行的LinearLayout的layout_height=0dp,layout_weight=1,具體設(shè)置如下:
<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>
這樣就保證了上下自適應(yīng)布局。然后行里面的Button也是平均分配,不過(guò)這里是橫向布局:layout_width=0dp,layout_weight=1,具體設(shè)置如下:
<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>
這樣就達(dá)到了上面的數(shù)字鍵盤(pán)的上下左右自適應(yīng)了?,F(xiàn)在的問(wèn)題是其中的灰色邊框怎么出來(lái)呢?TextView中沒(méi)有設(shè)置border的屬性,網(wǎng)上找的方法又很麻煩。
這里需要用到一個(gè)技巧,利用灰色背景,讓兩個(gè)按鍵間的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" />
為什么要設(shè)置layout_width=0dp?結(jié)合layout_weight,可以使控件成正比例顯示,輕松解決了當(dāng)前Android開(kāi)發(fā)最為頭疼的碎片化問(wèn)題之一。如果設(shè)置成wrap_content,內(nèi)容過(guò)長(zhǎng)會(huì)導(dǎo)致上下無(wú)法對(duì)齊的情況。
下面為整個(gè)布局內(nèi)容:
<?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="儲(chǔ)值"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
是不是很酷?
圖標(biāo)什么的自己上網(wǎng)找吧,這里就不貼出來(lái)了。
Android制作漂亮自適布局鍵盤(pán)的方法就先給大家介紹到這里,后續(xù)還會(huì)持續(xù)更新相關(guān)知識(shí),希望大家繼續(xù)關(guān)注腳本之家網(wǎng)站,謝謝!
- 解析android中隱藏與顯示軟鍵盤(pán)及不自動(dòng)彈出鍵盤(pán)的實(shí)現(xiàn)方法
- Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤(pán)
- Android 顯示和隱藏軟鍵盤(pán)的方法(手動(dòng))
- Android 軟鍵盤(pán)彈出時(shí)把原來(lái)布局頂上去的解決方法
- 5種方法完美解決android軟鍵盤(pán)擋住輸入框方法詳解
- Android鍵盤(pán)顯示與隱藏代碼
- Android軟鍵盤(pán)遮擋的四種完美解決方案
- Android實(shí)現(xiàn)彈出鍵盤(pán)的方法
- Android中監(jiān)聽(tīng)軟鍵盤(pán)顯示狀態(tài)實(shí)現(xiàn)代碼
- Android物理鍵盤(pán)事件解析
相關(guān)文章
SeekBar拖動(dòng)條的應(yīng)用實(shí)例
這篇文章主要為大家詳細(xì)介紹了SeekBar拖動(dòng)條的應(yīng)用實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
詳解如何在A(yíng)ndroid中實(shí)現(xiàn)懸浮Activity
本篇文章主要介紹詳解如何在A(yíng)ndroid中實(shí)現(xiàn)懸浮Activity,通過(guò)修改Activity的實(shí)現(xiàn)來(lái)適配平板設(shè)備,已達(dá)到代碼的最大利用率。有興趣的可以了解一下。2017-01-01
Android如何實(shí)現(xiàn)藍(lán)牙配對(duì)連接功能
Android 并沒(méi)有開(kāi)放配對(duì)連接耳機(jī)的接口,而且網(wǎng)上大部分資料都是講解如何連接藍(lán)牙4.0的,很少有資料詳細(xì)介紹藍(lán)牙2.0相關(guān)的。期間還是踩了不少坑才摸索出解決辦法。所以把我自己摸索總結(jié)出來(lái)的經(jīng)驗(yàn)梳理記錄下,以便備份2021-05-05
Android使用Theme自定義Activity進(jìn)入退出動(dòng)畫(huà)的方法
這篇文章主要介紹了Android使用Theme自定義Activity進(jìn)入退出動(dòng)畫(huà)的方法,涉及Android的Activity屬性設(shè)置與資源操作技巧,需要的朋友可以參考下2016-07-07
利用Android中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫(huà)
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話(huà),每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫(huà)。那如何顯示這個(gè)效果,下面一起來(lái)看看。2016-08-08
Android實(shí)現(xiàn)圖片左右滑動(dòng)效果
現(xiàn)在滑動(dòng)效果用的比較多,尤其是在手機(jī)端上面,本文介紹了Android實(shí)現(xiàn)圖片左右滑動(dòng)效果,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-10-10
詳解Android WebView的input上傳照片的兼容問(wèn)題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)右邊抽屜Drawerlayout效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

