我通过WindowManager将叠加布局添加到屏幕上,我在那里有android:theme
参数的按钮:
FrameLayout view = (FrameLayout) View.inflate(this, R.layout.button_view, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT
);
windowManager.addView(view, params)
button_view
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme">
<Button
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/button_text"
android:theme="@style/AppTheme"
tools:ignore="UnusedAttribute" />
</FrameLayout>
和AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="android:windowBackground">@color/white</item>
</style>
颜色color_accent
为蓝色,在api 23-25的模拟器上,按钮颜色也为蓝色。
但是在使用api 16的模拟器上,它是浅灰色的,在模拟器上使用api 22,它是红色的。
我该如何解决?
当我将具有此主题的此按钮添加到活动的布局时,它可以正常工作。