app:showAsAction =“ifRoom”和android:showAsAction =“ifRoom”都没有按预期工作

时间:2017-11-22 11:11:22

标签: android kotlin android-support-library

我有一个定义为

的选项菜单
<?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/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/ic_search_white_24dp"
      android:orderInCategory="1"
      app:actionViewClass="android.support.v7.widget.SearchView"
      app:showAsAction="always|collapseActionView"/>

  <item 
      android:id="@+id/action_favorite"
      android:title="@string/action_favorite_add"
      android:icon="@drawable/ic_favorite_border_white_24dp"
      android:orderInCategory="2"
      app:showAsAction="ifRoom"
      />

  <item 
      android:id="@+id/action_info"
      android:title="@string/action_info"
      android:icon="@drawable/ic_info_outline_white_24dp"
      android:orderInCategory="3"
      app:showAsAction="ifRoom"
      />
</menu>

选项菜单本身可以正常工作,但是当我有一个较长的工具栏标题时,ifRoom操作不会折叠为溢出。相反,工具栏标题会被截断。

如果我使用android:showAsAction而不是app:showAsAction而忽略了有关支持库的错误,那么这些项目总是会折叠为溢出。无论标题的长度如何。

我还应该提一下,工具栏位于可折叠的工具栏中

我想让搜索操作项始终显示(无论标题的长度如何),只有在工具栏标题没有被截断时才显示其他两项。

更新:布局和onCreate

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/white">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/rm_app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_gravity="top"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/rm_collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:titleEnabled="false"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary"
            app:statusBarScrim="?attr/colorPrimary"
            >

            <RelativeLayout
                android:id="@+id/rm_expanded_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:id="@+id/rm_img_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:imagePlaceholder="@{@drawable/img_placeholder}"
                    app:imageUrl="@{vm.imageUrl}"
                    android:scaleType="centerCrop"
                    />

                <!-- Some other layouts overlayed over the image view -->

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/rm_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@{vm.name}"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- rest of layout -->

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

的onCreate

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  activityComponent().inject(this)

  setAndBindContentView(R.layout.activity_some_activity, savedInstanceState)

  setTranslucentStatusBar()
  setSupportActionBar(binding.rmToolbar)
  supportActionBar?.let {
    it.setDisplayHomeAsUpEnabled(true)
    it.setDisplayShowTitleEnabled(true)
  }

  binding.rmAppBarLayout.addOnOffsetChangedListener(object: AppBarStateChangeListener() {
    override fun onStateChanged(appBarLayout: AppBarLayout?, state: State) {
      if (state != State.IDLE) { // collapsed or expanded
        invalidateOptionsMenu()
      }
    }
  })

  // some other view code

  // viewModel load code
}

1 个答案:

答案 0 :(得分:0)

请尝试这个希望,这样对你有所帮助。我不知道这是否有效,但就我而言:

<?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/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/ic_search_white_24dp"
      android:orderInCategory="1"
      app:actionViewClass="android.support.v7.widget.SearchView"
      app:showAsAction="always|collapseActionView"/>

  <item 
      android:id="@+id/action_favorite"
      android:title="@string/action_favorite_add"
      android:icon="@drawable/ic_favorite_border_white_24dp"
      android:orderInCategory="2"
      app:showAsAction="collapseActionView"
      />

  <item 
      android:id="@+id/action_info"
      android:title="@string/action_info"
      android:icon="@drawable/ic_info_outline_white_24dp"
      android:orderInCategory="3"
      app:showAsAction="collapseActionView"
      />
</menu>