【问题标题】:The space between the back button and the search view is too much - is it possible to reduce it on the toolbar?后退按钮和搜索视图之间的空间太大 - 是否可以在工具栏上减少它?
【发布时间】:2018-12-29 16:33:50
【问题描述】:

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;
    }

我要显示的内容

显示的内容

android中后退按钮和搜索视图之间的空间比较大,可以减少吗?

【问题讨论】:

  • 我也面临同样的问题。您对此有什么解决办法吗?

标签: android


【解决方案1】:

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

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

原答案是here

【讨论】:

    【解决方案2】:

    更改菜单 xml 的 showAsAction。这正是您正在寻找的。​​p>

    app:showAsAction="ifRoom|collapseActionView"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多