android - 很难为 Android 4 中的 AppCompat 正确设置工具栏操作模式的样式

标签 android

在我的 Android 5 中,使用以下主题

主题.xml

<style name="Theme.MyApp.Light.DarkActionBar" parent="Theme.AppCompat.Light.NoActionBar">

以及下面的工具栏

工具栏.xml

<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/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/background_material_dark"
    android:elevation="4dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

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

这就是当我的 ListViewFragment 通过进入操作模式时我得到的结果

this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
this.getListView().setMultiChoiceModeListener(new ModeCallback());

enter image description here

我希望我的 Android 4 拥有准确的外观和感觉。

如果我们在 Android 4 中不做任何事情,操作模式下的工具栏看起来会非常错误。

enter image description here

这似乎是 Android 支持库中的错误:https://code.google.com/p/android/issues/detail?id=143419&thanks=143419&ts=1423493133

我曾提到Styling ActionMode ActionBar in Android 5.0 Lollipop (with AppCompat)

<style name="Theme.MyApp.Light.DarkActionBar" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="actionModeBackground">@color/background_material_dark</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>

这就是我得到的。

enter image description here

  1. 文字颜色不正确
  2. 没有绿色分隔符。
  3. 返回图标颜色不正确。

我进一步关注Unable to style action mode when using Toolbar

<style name="Widget.ActionMode" parent="@style/Widget.AppCompat.ActionMode">
    <item name="background">@color/background_material_dark</item>
    <item name="backgroundSplit">@color/accent_material_light</item>
    <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title.Inverse</item>
    <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle.Inverse</item>
</style>

<style name="Theme.MyApp.Light.DarkActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="actionModeStyle">@style/Widget.ActionMode</item>
    <item name="actionModeCloseDrawable">@drawable/ic_arrow_back_white_24dp</item>

它正在变得更好(背面图标的颜色正确)

但是,我仍然面临问题。

  1. 文字颜色不正确
  2. 没有绿色分隔符。

enter image description here

我想知道,在 Android 5 中获得正确外观和感觉的正确方法是什么?

最佳答案

我在 AppCompat v22.1.1 中遇到了类似的问题,并提出了以下解决方案。

第 1 步。您的主题应将 Theme.AppCompat.Light.DarkActionBar 作为父主题,如下所示

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <!-- For same effect as Theme.AppCompat.Light.NoActionBar -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

第 2 步。将以下行添加到您的工具栏。

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

您不必在最新的 AppCompat 库 (22.1.1) 中使用 app:theme

通过上述解决方案,我能够在工具栏和操作模式中获取白色图标和文本。在其他解决方案中,工具栏中的图标和文本是白色的,但弹出窗口、对话框等使用深色主题,这看起来很奇怪,除非你真的想要它。我希望这会有所帮助。

关于android - 很难为 Android 4 中的 AppCompat 正确设置工具栏操作模式的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763247/

相关文章:

android - 用于创建此效果的最佳布局组合是什么?

android - Libgdx:屏幕管理器

android - 如何获取图像资源 ID 并将其发送到 Android 中的其他 Activity ?

java - Android中的车辆跟踪系统

android - 在 Android 中将已解析的 Xml 数据显示到 ListView

android - 当我的 View 滚动出可见区域时如何收听

android - 滚动 ListView 后无法将项目添加到 ListView

java - Android 客户端套接字 IOException

java - 如何应对交互式 Linux 命令?

android - 给出非法状态异常的 http post