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

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="標題欄"
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="請輸入搜索內(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的特性來完成的效果,并沒有添加其他什么邏輯。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題
這篇文章主要介紹了基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android使用GridView實現(xiàn)日歷的方法
本篇文章主要介紹了Android使用GridView實現(xiàn)日歷的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析
這篇文章主要介紹了Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Android Bitmap的加載優(yōu)化與Cache相關(guān)介紹
這篇文章主要介紹了Android中性能優(yōu)化之Bitmap的加載優(yōu)化與Cache相關(guān)內(nèi)容介紹,文中介紹的很詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-02-02
解決android Listview的item中最外層Margin失效的問題
下面小編就為大家?guī)硪黄鉀Qandroid Listview的item中最外層Margin失效的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析
這篇文章主要給大家介紹了關(guān)于Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

