如果是设置activity的非透明主题,可以通过setTheme动态设置,但是如果设置透明主题,通过这种方式不会生效,系统似乎忽略了windowIsTranslucent=true这个属性设置。
例如你的appcation样式如下:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- "colorPrimary"
>@color/colorPrimary
- "colorPrimaryDark"
>@color/colorPrimaryDark
- "colorAccent">@color/colorAccent
- "colorControlHighlight">@color/a_bg_color_dddddd
style>
透明样式如下:
<style name="AppThemeDynamic" parent="AppTheme">
- "android:windowBackground"
>@color/transparent
- "android:windowIsTranslucent"
>true
style>
如果需要动态设置activity透明背景需要先在Androidmainfest清单文件中先设置透明主题:
<application
android:theme="@style/AppTheme"
android:name=".ChatApp"
android:allowBackup="false"
android:resizeableActivity="false"
android:icon="@drawable/icon_logo"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:persistent="true"
android:supportsRtl="true"
android:largeHeap="true"
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
tools:replace="android:icon,android:allowBackup">
<activity
android:name=".activity.UrlActivity"
android:hardwareAccelerated="true"
android:theme="@style/AppThemeDynamic"
android:screenOrientation="portrait" />
application>
动态设置代码如下:
//是否需要透明
override fun onCreate(savedInstanceState: Bundle?) {
val isTransparent = if ("如果需要透明"){
setTheme(R.style.AppThemeDynamic)
true
}else{
setTheme(R.style.AppTheme)
false
}
super.onCreate(savedInstanceState)
}