【发布时间】:2020-07-14 12:53:48
【问题描述】:
或类似的。
我有一个带有片段容器的活动。每个片段布局本身没有背景。我已经尝试将背景设置为透明 - 没有效果。 我也使用FragNav library 来处理片段事务。
问题是 - 在编辑器 xml 预览中,widnows 背景正确显示。
风格:
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@drawable/ng_window_background</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>
可绘制的“ng_windows_background”只是一个纯色的形状。颜色 HEX 是 #FDA23。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/colorAccent"/>
</shape>
XML preview example:背景色为橙色。
Screenshot from device 可以看到,背景颜色是灰色的。
我已经尝试过的:
- 更改片段的布局背景 (android:background) 颜色。是的,它有效,但它会导致过度绘制效果,我试图通过使用 android:windowBackground 属性来避免。
- 由于材质组件建议,使用 android:colorBackground 属性。没有效果。
- 以编程方式和清单更改应用程序的主题。没有效果。
- 将 Theme.MaterialComponents.NoActionBar 更改为 AppCompat。没有效果 + 由于某些问题,我无法使用 AppCompat。
- 通过getWindow().setBackgroundResource()改变背景; OnResume() 中都没有;
我猜这是 MaterialComponents lib 的问题。我猜。
'如有必要,可以显示任何一段代码。
编辑:清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="packageName"
>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="AppClassPath"
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/logo_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=SplashScreenPath
android:theme="@style/AppTheme.Launcher">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name=MainActivityPath />
</application>
</manifest>
编辑#2: 删除了任何功能,将 OnCreate() 留空。没有效果,所以外部库没有问题。
帮助
【问题讨论】:
-
你试过 getWindow().setBackgroundDrawableResource(yourDrawable) 吗?
-
无效。我觉得这是一个材料组件问题,因为我见过类似的问题,但是使用 MaterialButton。或者这是 FragNav 库的问题,不知道。困扰我的是窗口背景实际上在 XML 预览中被更改。
-
是的,它发生了,你看到 XML 中的一切都是完美的,但是在设备上测试它时。这没用。你到底想改变什么背景?活动?还是一个片段?您是否尝试过在 onresume 中设置?
-
呃,一个应用程序?未设置布局背景时,会显示 windows 背景吗?我猜是默认主题背景,例如白色。将尝试在 OnResume() 中设置。谢谢。
-
onResume() 无效。
标签: java android material-design