android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼
APP中經(jīng)常會(huì)使用到搜索功能,那我們的搜索框該如何布局呢?下面咱們就看一下下面這個(gè)效果:

xml布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/large_text" />
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:title="標(biāo)題欄"
app:navigationIcon="@mipmap/navigation_back_white"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<RelativeLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="@color/colorPrimary"
android:padding="16dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_menu_camera"
android:drawablePadding="22dp"
android:drawableRight="@android:drawable/ic_menu_search"
android:gravity="left|center"
android:hint="請(qǐng)輸入搜索內(nèi)容"
android:padding="10dp"
android:textColorHint="@android:color/darker_gray" />
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Activity直接引用布局即可:
public class StopSearchViewBelowToolbarActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_search_view_toolbar_layout);
}
}
這里就是充分使用了CoordinatorLayout、AppBarLayout、NestedScrollView、Toolbar的特性來(lái)完成的效果,并沒(méi)有添加其他什么邏輯。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點(diǎn)問(wèn)題
這篇文章主要介紹了基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點(diǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android使用GridView實(shí)現(xiàn)日歷的方法
本篇文章主要介紹了Android使用GridView實(shí)現(xiàn)日歷的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析
這篇文章主要介紹了Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android Bitmap的加載優(yōu)化與Cache相關(guān)介紹
這篇文章主要介紹了Android中性能優(yōu)化之Bitmap的加載優(yōu)化與Cache相關(guān)內(nèi)容介紹,文中介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02
解決android Listview的item中最外層Margin失效的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決android Listview的item中最外層Margin失效的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android實(shí)現(xiàn)微信自動(dòng)搶紅包的程序
簡(jiǎn)單實(shí)現(xiàn)了微信自動(dòng)搶紅包的服務(wù),原理就是根據(jù)關(guān)鍵字找到相應(yīng)的View, 然后自動(dòng)點(diǎn)擊,接下來(lái)通過(guò)本文給大家介紹Android實(shí)現(xiàn)微信自動(dòng)搶紅包的程序,對(duì)android微信自動(dòng)搶紅包相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-02-02
Flutter啟動(dòng)頁(yè)(閃屏頁(yè))的具體實(shí)現(xiàn)及原理詳析
這篇文章主要給大家介紹了關(guān)于Flutter啟動(dòng)頁(yè)(閃屏頁(yè))的具體實(shí)現(xiàn)及原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

