后退按钮和搜索视图之间的空间太大-是否可以在工具栏上缩小它?

时间:2018-12-29 16:33:51

标签: android

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content" xmlns:card_view="http://schemas.android.com/tools"
              android:orientation="vertical"
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center"
            app:theme="@style/ToolBarStyle">

        <TextView
                android:id="@+id/toolbar_title"
                style="@style/style_toolbar_textView" />

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

    <View style="@style/style_toolbar_shadow" />
</LinearLayout>

menu_events_ppl_list.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menuSearchId"
          app:showAsAction="always"
          android:icon="@drawable/search_material"
          android:title="@string/str_search"
          app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

代码

/** Init On create options menu **/
    private fun initOnCreateOptionsMenu(menu: Menu) {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.menu_events_ppl_list, menu)

        val search = toolbar.menu.findItem(R.id.menuSearchId).actionView as SearchView
        search.queryHint = "Search People";
        //search.setOnQueryTextListener(this);
        search.isIconified = false
        search.maxWidth = Integer.MAX_VALUE;
    }

我要显示的内容

enter image description here

显示的内容

enter image description here

在Android中,后退按钮和搜索视图之间的空间更大,有可能减少它吗?

2 个答案:

答案 0 :(得分:0)

更改菜单xml的showAsAction。这正是您想要的。

app:showAsAction="ifRoom|collapseActionView"

答案 1 :(得分:0)

默认情况下,工具栏的后退按钮后有16dp插入。因此,在工具栏中添加app:contentInsetStartWithNavigation="0dp",它将删除该空白。

<androidx.appcompat.widget.Toolbar
   ..........
   ..........
   app:contentInsetStartWithNavigation="0dp"
   ..........
   ..........
</androidx.appcompat.widget.Toolbar>

原始答案是here