Android Studio實(shí)現(xiàn)搜索欄
前言
一、何為自定義控件?
系統(tǒng)自帶的控件都是直接或間接繼承自View的,View是Android中最基本的UI組件庫(kù)。當(dāng)系統(tǒng)的自帶控件無法滿足需求時(shí),依據(jù)控件的繼承結(jié)構(gòu)來制作自定義控件,例如:頂部搜索欄
二、制作步驟
1.引入布局
如果在每一個(gè)Activity的布局中都編寫一個(gè)搜索欄,會(huì)導(dǎo)致代碼的重復(fù)。通過采用引入布局的形式,可以解決這個(gè)問題。
首先在layout目錄下創(chuàng)建一個(gè)select.xml布局,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/back001">
<ImageView
android:id="@+id/select_p01"
android:layout_marginTop="2dp"
android:layout_width="46dp"
android:layout_height="41dp"
android:src="@drawable/select_photo01" />
<EditText
android:id="@+id/select01"
android:layout_marginTop="6dp"
android:layout_width="250dp"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
<!--采用edittext_shap01樣式-->
android:background="@drawable/edittext_shape01"/>
<ImageButton
android:id="@+id/select_p02"
<!-- 清除按鈕背景-->
style="?android:attr/borderlessButtonStyle"
android:layout_marginTop="0dp"
android:layout_width="53dp"
android:layout_height="50dp"
<!-- fitXY 的作用是“保持圖片長(zhǎng)寬比例”-->
android:scaleType="fitXY"
android:src="@drawable/select_photo02" />
</LinearLayout>
2.布局解析
此處對(duì)上方的布局進(jìn)行補(bǔ)充:
1).設(shè)置布局背景為back001
android:background="@drawable/back001"

2).導(dǎo)入圖片select_photo01
為了使搜索欄不單調(diào),故導(dǎo)入圖片裝飾
android:src="@drawable/select_photo01"
3.
3.采用edittext_shape01樣式
系統(tǒng)自帶的EditText僅是一條直線,此處將其變成圓型輸入框
android:background="@drawable/edittext_shape01"
變化前:

變化后:

edittext_shape01代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#FFFFFF" >
</solid>
<!-- 設(shè)置圓角 -->
<corners
android:radius="3dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" >
</corners>
<!-- 設(shè)置邊框 -->
<stroke android:width="1dip" android:color="#ff000000" />
</shape>
4.導(dǎo)入圖片select_photo02裝飾
android:scaleType="fitXY" android:src="@drawable/select_photo02"
注:fitXY較為重要!

測(cè)試
完成布局的編寫后,便可以將select01作為自定義控件,進(jìn)行調(diào)用:
<include layout="@layout/select01" />
只需要通過一行include語句,便可以引入搜索欄布局
引入前后對(duì)比:
引入前

引入后

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)Jetpack組件Room使用講解
Room是一個(gè)數(shù)據(jù)庫(kù)訪問組件; 對(duì)SqLite數(shù)據(jù)庫(kù)做了友好的封裝,使我們?cè)诰幋a的時(shí)候,只需要注重邏輯的部分即可,數(shù)據(jù)庫(kù)就交給Room去流暢的訪問即可2022-08-08
Android實(shí)現(xiàn)ListView分頁(yè)加載數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ListView分頁(yè)加載數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Android使用GridView實(shí)現(xiàn)日歷的簡(jiǎn)單功能
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)日歷的簡(jiǎn)單功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
教你五分鐘實(shí)現(xiàn)Android超漂亮的刻度輪播控件實(shí)例教程
說到輪播圖,想必大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于如何利用五分鐘快速實(shí)現(xiàn)一款超漂亮的Android刻度輪播控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-09-09
Android自定義圓形View實(shí)現(xiàn)小球跟隨手指移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義圓形View實(shí)現(xiàn)小球跟隨手指移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android錄制語音文件wav轉(zhuǎn)mp3的方法示例
這篇文章主要介紹了Android錄制語音文件wav轉(zhuǎn)mp3的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Android編程顯示網(wǎng)絡(luò)上的圖片實(shí)例詳解
這篇文章主要介紹了Android編程顯示網(wǎng)絡(luò)上的圖片,結(jié)合實(shí)例形式詳細(xì)分析了Android顯示網(wǎng)絡(luò)圖片的流程與具體操作技巧,需要的朋友可以參考下2016-10-10

