前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android顶部(toolbar)搜索框实现代码

android顶部(toolbar)搜索框实现代码

作者头像
砸漏
发布2020-10-30 00:56:46
1.2K0
发布2020-10-30 00:56:46
举报
文章被收录于专栏:恩蓝脚本

APP中经常会使用到搜索功能,那我们的搜索框该如何布局呢?下面咱们就看一下下面这个效果:

xml布局:

代码语言:javascript
复制
<?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="请输入搜索内容"
        android:padding="10dp"
        android:textColorHint="@android:color/darker_gray" / 
    </RelativeLayout 

  </android.support.design.widget.AppBarLayout 

</android.support.design.widget.CoordinatorLayout 

Activity直接引用布局即可:

代码语言:javascript
复制
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的特性来完成的效果,并没有添加其他什么逻辑。

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档