自定义操作栏内容被选项菜单项部分隐藏

时间:2019-08-02 07:52:39

标签: android

我有一个自定义操作栏和一个选项菜单。如果仅将1个选项菜单项设置为app:showAsAction="ifRoom",操作栏将显示所有内容:

enter image description here

如果将另一个菜单项设置为“ ifRoom”,则包含电池百分比的TextView和Bluetooth图标消失:

enter image description here

用蓝色矩形覆盖的部分是应用程序标题,它始终存在。需要说明的是:电池百分比和“蓝牙”图标不是选项菜单项,而是位于自定义操作栏中。我不明白的是,如果所有项目都设置为app:showAsAction="ifRoom",为什么选项菜单会“修剪”操作栏。

选项菜单代码:

<?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/actionbar_button_config"
        android:title="@string/actionbar_button_config"
        android:icon="@drawable/outline_tune_24"
        android:enabled="false"
        app:showAsAction="ifRoom">
    </item>
    <item android:id="@+id/actionbar_button_reg_rw"
        android:title="Register R/W"
        android:icon="@drawable/ic_swap_vert_black_24dp"
        android:enabled="false"
        app:showAsAction="ifRoom">
    </item>
    <item android:id="@+id/actionbar_button_info"
        android:title="@string/actionbar_button_about"
        android:icon="@drawable/ic_info_outline_black_24dp"
        app:showAsAction="never">
    </item>
</menu>

自定义操作栏布局:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        style="@style/TextAppearance.AppCompat.Title"
        android:id="@+id/custom_actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/unknown"
        android:textColor="?android:textColorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/battery_level_textview_main_activity"
        android:layout_width="55dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:drawableEnd="@drawable/ic_battery_unknown_black_24dp"
        android:drawablePadding="-3dp"
        android:gravity="center_vertical|end"
        android:text="@string/value_unknown"
        app:layout_constraintTop_toTopOf="@id/custom_actionbar_title"
        app:layout_constraintBottom_toBottomOf="@id/custom_actionbar_title"
        app:layout_constraintStart_toEndOf="@id/custom_actionbar_title" />

    <ImageView
        android:id="@+id/ble_disconnected_iv"
        android:contentDescription="@string/ble_disconnected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@id/battery_level_textview_main_activity"
        app:layout_constraintStart_toStartOf="@id/battery_level_textview_main_activity"
        app:layout_constraintEnd_toEndOf="@id/battery_level_textview_main_activity"
        app:srcCompat="@drawable/ic_bluetooth_black_24dp" />

    <ImageView
        android:id="@+id/ble_connected_iv"
        android:contentDescription="@string/ble_connected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/battery_level_textview_main_activity"
        app:layout_constraintStart_toStartOf="@id/battery_level_textview_main_activity"
        app:layout_constraintEnd_toEndOf="@id/battery_level_textview_main_activity"
        app:srcCompat="@drawable/ic_bluetooth_connected_black_24dp" />

    <TextView
        android:id="@+id/custom_actionbar_subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        app:layout_constraintTop_toBottomOf="@id/custom_actionbar_title"
        app:layout_constraintStart_toStartOf="parent"
        android:text="@string/unknown_fw_version"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在活动中配置的操作栏:

        final Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayHomeAsUpEnabled(true);

        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomActionBar = mInflater.inflate(R.layout.custom_actionbar, null);
        TextView mActionbarTitle = mCustomActionBar.findViewById(R.id.custom_actionbar_title);
        TextView mActionbarSubtitle = mCustomActionBar.findViewById(R.id.custom_actionbar_subtitle);
        mActionbarTitle.setText(deviceName);
        mActionBar.setCustomView(mCustomActionBar);
        mActionBar.setDisplayShowCustomEnabled(true);

活动布局中的工具栏:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>

    </com.google.android.material.appbar.AppBarLayout>
    ...

任何想法,如何防止选项菜单项修剪自定义操作栏?

0 个答案:

没有答案