工具栏样式[标题居中、颜色]和SearchView中遇到的坑

《工具栏样式[标题居中、颜色]和SearchView中遇到的坑》 萌妹子.jpeg

在开发的过程中经常要用到工具栏和菜单栏,有时候你看到有些APP的工具栏多么炫酷,然你也想做一个类似的,发现在做的过程中会遇到许多的坑,至于那些坑只有你自己去通过编码、调试、发现问题、查阅资料、解决问题一般开发都需要经过这个过程。所以在此将此知识点通过博客的形式记录下来。

  • 工具栏文字样式设置:居中颜色大小等属性设置
  1. 设置工具栏的Style
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize">


    <TextView
        android:id="@+id/tv_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        android:layout_gravity="center"
        android:text="Toolbar Title"/>

</android.support.v7.widget.Toolbar>

需要注意的几点是:

  • Toolbar可以通过android:background设置背景色。
  • Toolbar可以通过layout_height设置工具栏的高度,上面是设置为系统AcitonBar默认大小,我个人习惯设置为50dp。
  • 工具栏的各种属性可以通过设置TextView的属性来展现到UIlayout_gravity属性相对于父布局也就是这里的Toolbar来说的,而gravity属性是相对于自己控件本身的布局。
  1. 代码设置工具栏的标题等属性
tvToolbar.setText(title);//set toolbar textView content
  • 如何隐藏系统自带的工具栏?
  1. 通过配置XML实现
  • 设置界面全屏效果
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  • 设置无标题
android:theme="@android:style/Theme.NoTitleBar"  

本Demo是统一设置所有页面为无标题,通过设置Style

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

然后添加到AndroidManifest.xml的App的theme属性中

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
  1. 通过代码实现
  • 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);  

  • 设置无标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);  

注意:这些都是设置代码需要写在调用设置布局文件之前

  • 如何在工具栏上优雅地使用菜单栏(SearchView)
  1. 工具栏配置文件XML
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@android:string/search_go"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom"/>
</menu>

*注意:这里一定要设置app:actionViewClass="android.support.v7.widget.SearchView"不然实例化获取到的对象为nullapp:showAsAction设置有collapseActionView属性,searchView菜单栏才会有返回箭头

  1. 使用工具栏的配置文件XML
<include layout="@layout/view_toolbar"/>
  1. 加载工具栏的Menu
//toolbar add menu
toolbar.inflateMenu(R.menu.menu_search);
toolbar.setOnMenuItemClickListener(onMenuItemClickListener);

//toolbar search menu click listener
private Toolbar.OnMenuItemClickListener onMenuItemClickListener = new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        //charge the id is equal what you want
        if (item.getItemId() == R.id.action_search) {
            queryData(item);
        }
        return false;
    }
};
  • 不使用系统菜单栏会导致一些菜单栏相关函数OverrideonPrepareOptionsMenuonOptionsItemSelected等函数不调用如何解决?
  1. 基于Fragment页面
    onCreate设置setHasOptionsMenu(true)
    onViewCreated设置getActivity().invalidateOptionsMenu()
    但是我测试发现并没有没有调用onPrepareOptionsMenu等函数,这个后续查阅资料stackOverFlow这个好像没有解决此问题
  2. 基于Activity页面
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

也可以像设置前面设置APP所有页面无标题一样设置Activity

<!-- Activity theme. -->
<style name="ActivityTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

然后设置到AndroidManifest.xml的指定的Activity的theme属性中

 <activity
    android:name=".SearchViewActivity"
    android:theme="@style/ActivityTheme">
</activity>
  • 使用SearchView过程中你需要注意的问题和一个常见的BackPress退出问题
  1. 开启系统搜索服务
SearchManager manager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
  1. 实例化SearchView对象
mSearchView = (SearchView) item.getActionView();
  1. 设置setOnQueryTextListener事件监听搜索条件
//on query text listener
private SearchView.OnQueryTextListener onQueryTextListener = new SearchView.OnQueryTextListener() {
    @Override //submit the search condition
    public boolean onQueryTextSubmit(String query) {
        Toast.makeText(getContext(), "Search Data in fragment", Toast.LENGTH_SHORT).show();
        return false;
    }
    @Override //the search condition once changed will invoke this funtion
    public boolean onQueryTextChange(String newText) {
    return false;
    }
};
  1. 设置setOnQueryTextFocusChangeListener事件监听输入框焦点的变化通过此函数解决了OnBackPressed直接退出APP的问题
//the focus of the query condition box once changed , will call this funtion
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        //charge the focus is changed and the searchview icon isn't iconified
        if (!focused && !mSearchView.isIconified()) {
            //in the fragment you mus you this way can solve it
            MenuItemCompat.collapseActionView(item);
        }
    }
});
  1. 网上说的关闭活动的SearchView时的解决办法是override onBackPressed函数
@Override
public void onBackPressed() {
    if (!searchView.isIconified()) {
        searchView.setIconified(true);
    } else {
        super.onBackPressed();
    }
}

在Fragment中当然没有这个函数可以让你重写,但是可以通过在基类implement OnBackPressed(自己定义的)接口详情请见onBackPressed() for Fragments,我通过此方法,使用上面的这种发现效果图如before.gif并不是解决办法,还是监听setOnQueryTextFocusChangeListener是正确的,正确图片如下perfect.gif

《工具栏样式[标题居中、颜色]和SearchView中遇到的坑》 before.gif

《工具栏样式[标题居中、颜色]和SearchView中遇到的坑》 perfect.gif

  • 关于Toolbar使用的一些思考和总结
  1. 网络上此类型的提问太多,回答也太多,至于真正正确的答案需要你自己动手编码,自己经过测试才知道。
  2. Toolbar是受到系统样式的约束,如果自己设置了NoActionBar ,FullScreen等样式的话就会没有工具栏。
  3. 你使用系统默认的样式<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">,然后在每个页面有自己定义的toolbar的话,这样会导致有两个工具栏[自己可以测试]。
  4. 题外简书如何生成目录,网络上找的在后面添加[toc]貌似没用。
  • 参考链接

如何使用菜单Menu

如何使用SearchView

Android系统自带样式

Implementing SearchView in action bar

Android toolbar center title and custom font

How do I close a SearchView programmatically?

onPrepareOptionsMenu not getting called in Fragments

希望大家多多关注star and fork,各位觉得如果有帮助给个赞,谢谢
    原文作者:LelandACM
    原文地址: https://www.jianshu.com/p/572d36f8f97d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞