以编程方式设置 android:windowIsTranslucent

问题描述 投票:0回答:7

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

但是,为此,我必须在

styles.xml
中添加这一行:

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

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

Activity.onAttachedToWindow()
左右...)

预先感谢您的帮助。

[编辑 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);

但是没有任何效果...

android android-layout android-theme android-styles android-windowmanager
7个回答
6
投票

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

恐怕不行。您只需在

styles.xml
内完成即可。据我所知
android:windowIsTranslucent
的值无法通过编程单独更改。

当我们调用

super.onCreate(savedInstanceState);
时,Activity 类提供了一个空的图形窗口,我们可以在其中设置内容,即视图。并将主题应用于该窗口,然后将内容加载到该视图上。

因此顺序将是,

  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>

然后为

Floating activity
设置主题,如下所示:

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

3
投票

这样做

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     Window w = getWindow(); // in Activity's onCreate() for instance
     w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, 
     WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
     w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, 
     WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

3
投票

Android R 出现新方法


1
投票
不能说要以编程方式执行此操作..但如果您需要执行此操作,您可以像这样尝试..

将主题添加到清单文件中的活动中,如下所示..

android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"

示例:-

<activity android:name="com.example.androidhackerphonelocker.MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

希望它能解决您的一些问题..


0
投票
也许这会有所帮助。

@Override protected void onCreate(Bundle savedInstanceState) { processSetTheme(this); getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().setBackgroundDrawableResource(R.color.realTranslucent); if (Build.VERSION.SDK_INT>=19){ getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } super.onCreate(savedInstanceState); <color name="realTranslucent">#00000000</color>
    

0
投票
如果 android.app.Dialog

window.setDimAmount(0f) window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    

0
投票
从 API 级别 30 开始,现在有一个

setTranslucent

 方法:

将活动(尤其是具有

R.attr.windowIsTranslucent

R.attr.windowIsFloating
 属性的活动)转换为全屏不透明活动,或将其从不透明转换回半透明。

文档

© www.soinside.com 2019 - 2024. All rights reserved.