android - 以编程方式设置 android :windowIsTranslucent

标签 android android-layout android-theme android-styles android-windowmanager

在完成本教程后,我能够创建一个 float Activity http://cases.azoft.com/android-tutorial-floating-activity/

但是,为此,我必须在 styles.xml 中添加这一行:

<item name="android:windowIsTranslucent">true</item>

仅使用 Android/Java 代码是否可以达到相同的效果? (例如在 Activity.onAttachedToWindow() 左右...)

预先感谢您的帮助。

[EDIT 01] styles.xml 不得更改(我不应该知道其中的内容...)。但出于测试目的,我使用默认的:

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>

[编辑 02] Resources.Theme.applyStyle() 似乎做我想做的事(根据 API 描述:“将新的属性值放入主题中”)。 所以我创建了以下 custom_style.xml :

<resources>
    <style name="MyCustomStyle" >
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

然后,在 onAttachedToWindow() 中,我调用了:

getTheme().applyStyle(R.style.MyCustomStyle, true);

但是没有任何效果...

最佳答案

Is it possible to have the same effect using only Android/Java code ?

恐怕不行。您只能在 styles.xml 中执行此操作。 android:windowIsTranslucent 的 AFAIK 值不能以编程方式单独更改。

当我们调用 super.onCreate(savedInstanceState); 时,Activity 类会提供空的图形窗口,我们可以在其上设置我们的内容 ie.views。然后将主题应用于此窗口,然后将内容加载到此 View 中。

因此顺序是,

  1. 调用 super.onCreate()
  2. 为 Activity 设置主题。
  3. 为该 Activity 设置内容 View 。

例如。

styles.xml

<style name="AppTheme" parent="AppBaseTheme">

<!-- All customizations that are NOT specific to a particular API-level can go here. -->

    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:windowNoTitle">true</item>
</style>

<!-- Application theme.without title bar -->
<style name="AppTheme.NoTitleBar" parent="AppBaseTheme">

<!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:windowNoTitle">true</item>
</style>

<!--Floating activity theme -->
<style name="Theme_Translucent" parent="android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowFullscreen">true</item>
</style>

然后为 Floating activity 设置主题如下:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setTheme(R.style.Theme_Translucent); // Set here
    setContentView(...)
}

关于android - 以编程方式设置 android :windowIsTranslucent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24951852/

相关文章:

java - fragment 未显示其完整宽度和高度

android - ViewPager 内的多个 NestedScrollViews 不滚动

android - 根据当前设置的主题获取属性颜色值

java - JSONObject toString 和 Base64 性能

java - 如何在ViewText上添加换行符?

android - getActivity().runOnUiThreadnew Runnable() 出错

android - 如何以受控方式模拟 Android 手机的电池消耗?

android - 当引用的 View 在 ConstraintLayout 中消失时保持边距

android - 尝试更改 Android ProgressBar 的圆角半径时出错

android - 创建可下载的自定义主题并在运行时应用它