android - 如何自定义 float ActionBar的背景?

标签 android android-actionbar android-theme android-styles android-actionmode

在 Marshmallow 下,我们得到了复制/过去的顶部操作栏(感谢上帝),我们有一个 float 的操作栏。我现在的问题是如何将我的 float 操作栏的背景和文本颜色自定义为黑色而不是白色(不更改我的应用程序的完整主题,即 Theme.material.light.NoactionBar)

enter image description here

最佳答案

我认为这是不可能的。经过长时间深入研究 android 源代码后,我发现工具栏实现了一个 android 内部类 com.android.internal.widget.FloatingToolbar.FloatingToolbarPopup

弹出容器从名为 com.android.internal.R.layout.floating_popup_container 的布局膨胀,它使用此背景 android:background="?attr/floatingToolbarPopupBackgroundDrawable".

似乎没有办法从xml样式设置属性floatingToolbarPopupBackgroundDrawable。

但是,您可以使用 java 反射更改 Activity 类的背景

@Override
public void onActionModeStarted(ActionMode mode)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        try
        {
            Field field = mode.getClass().getDeclaredField("mFloatingToolbar");
            field.setAccessible(true);
            Object mFloatingToolbar = field.get(mode);
            field = mFloatingToolbar.getClass().getDeclaredField("mPopup");
            field.setAccessible(true);
            Object mPopup = field.get(mFloatingToolbar);
            field = mPopup.getClass().getDeclaredField("mContentContainer");
            field.setAccessible(true);
            ViewGroup mContentContainer = (ViewGroup)field.get(mPopup);
            mContentContainer.setBackgroundColor(Color.RED);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    super.onActionModeStarted(mode);
}

关于android - 如何自定义 float ActionBar的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39570788/

相关文章:

android - 同步错误 : com. google.android.gms :play-services-base:17. 2.0

android - 我无法更改操作栏中字幕的颜色

android - (AppCompat) 没有 UI 的 Activity

java - Microsoft ProjectOxford Vision API 错误

android - 改造.改造错误: 404 Not Found

android - 在工具栏右侧添加自定义 View

Android Espresso typeText 到 ActionBar 中的 EditText

android - 在 ListView android中更改渐变边缘颜色

java.lang.IllegalArgumentException : You need to use a Theme. AppCompat 主题(或后代)与设计库

android - 无法在处理中找到连接的设备