【问题标题】:Programmatically set android:windowIsTranslucent以编程方式设置 android:windowIsTranslucent
【发布时间】:2014-07-25 08:57:56
【问题描述】:

按照本教程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>

[EDIT 02] Resources.Theme.applyStyle() 似乎做了我想做的事(根据 API 描述:"Place new attribute values into the theme")。 所以我创建了以下custom_style.xml

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

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

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

但它没有任何效果......

【问题讨论】:

  • 您能找到解决方案吗?我的应用程序有 20 个主题,我不想仅仅为了添加这个属性而复制它们......
  • 抱歉,我没有找到我希望的解决方案。相反,我使用Xposed framework 破解了这个限制...我不建议您为您的应用程序这样做。
  • 谢谢。我的解决方案是扩展我的完整活动(其中没有额外的功能)并在清单中定义两者。一个没有半透明主题的。然后我决定在运行时启动哪个,覆盖活动的主题(半透明将始终保持在清单中使用的主题中定义的状态),就是这样......实际上,复制主题也不起作用,因为它似乎确实使用了清单中使用的主题中的 trnslucent 属性,所以总是...

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


【解决方案1】:

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

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

当我们调用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(...)
}

【讨论】:

  • 为什么不呢?另外,如果真的不可能以编程方式进行,我是否至少可以创建另一个主题(在另一个 xml 文件中)具有 android:windowIsTranslucent 项,然后使用它来“包装”原始样式(我不能更改原始 style.xml )。
  • 是的,您可以通过创建另一个具有 android:windowIsTranslucent 项目的主题来实现它。
  • 感谢您的示例,但这将替换原来的styles.xml。我不能追加我的自定义样式吗?我发现getTheme().applyStyle()(根据 API 描述“将新属性值放入主题中”)似乎可以满足我的要求。但是,当我创建一个custom_style.xml(其中包含&lt;resources&gt;&lt;style name="MyCustomStyle"&gt;&lt;item name="android:windowIsTranslucent"&gt;true&lt;/item&gt;&lt;/style&gt;&lt;/resources&gt;)时,它不起作用。
  • 为了追加 custom_style.xml,我在onAttachedToWindow()中使用了getTheme().applyStyle(R.style.MyCustomStyle, true)
  • 请发布您的 style.xml
【解决方案2】:

这样做

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: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>
    

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

    【讨论】:

    • 这不包括任何关于半透明或浮动的内容。这是一个不透明的全屏主题,没有标题栏。问题是寻求帮助通过代码使浮动窗口半透明。
    【解决方案4】:

    也许这会有所帮助。

    @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>
    

    【讨论】:

    • 您可以添加至少一些关于您的代码如何回答问题的解释
    【解决方案5】:

    a new method出现在Android R

    【讨论】:

      【解决方案6】:

      如果是 android.app.Dialog

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

      【讨论】:

        猜你喜欢
        • 2021-11-21
        • 2018-11-26
        • 2011-04-18
        • 2012-07-18
        • 2016-02-15
        • 2014-07-26
        • 1970-01-01
        • 2011-06-06
        • 2012-02-12
        相关资源
        最近更新 更多