以编程方式设置android:windowIsTranslucent

mad*_*uri 3 android android-layout android-theme android-windowmanager android-styles

按照本教程http://cases.azoft.com/android-tutorial-floating-activity/后,我能够创建一个浮动活动.

但是,要这样做,我必须添加以下内容styles.xml:

<item name="android:windowIsTranslucent">true</item>
Run Code Online (Sandbox Code Playgroud)

是否可以仅使用Android/Java代码获得相同的效果?(例如在Activity.onAttachedToWindow()...中)

在此先感谢您的帮助.

[编辑01] styles.xml不能改变(我不应该知道它里面有什么......).但出于测试目的,我使用的是默认值:

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

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

<resources>
    <style name="MyCustomStyle" >
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后,onAttachedToWindow()我打电话给:

getTheme().applyStyle(R.style.MyCustomStyle, true);
Run Code Online (Sandbox Code Playgroud)

但它没有任何影响......

Rit*_*une 5

是否可以仅使用Android/Java代码获得相同的效果?

我很害怕,不,你必须这样做styles.xml.android:windowIsTranslucent单独的AFAIK值不能以编程方式更改.

当我们调用super.onCreate(savedInstanceState);Activity类时,提供了我们设置内容ie.views的空图形窗口.并且将一个主题应用于此窗口,然后在此视图上加载内容.

因此序列将是,

  1. 拨电至 super.onCreate()
  2. 为活动设置主题.
  3. 设置该活动的内容视图.

例如.

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>
Run Code Online (Sandbox Code Playgroud)

然后设置您的主题Floating activity如下:

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