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

博客介绍了APP中搜索框的布局方法。给出了xml布局代码,包括CoordinatorLayout、AppBarLayout等组件的使用,还展示了Activity直接引用布局的代码,通过这些代码充分利用组件特性完成搜索框布局,未添加额外逻辑。

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

1150c5e5c1334220620c1f300fae8326.gif

xml布局:

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:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="16dp"

android:text="@string/large_text" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/ThemeOverlay.AppCompat.Dark">

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" />

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">

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" />

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的特性来完成的效果,并没有添加其他什么逻辑。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值