Android 填充整个屏幕的半透明活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10481277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Translucent Activity filling the entire screen
提问by Xavi Gil
I'd like to have an activity (2) with translucent aspect over another activity (1), aligned at the top of the screen (4).
我想要一个活动 (2) 与另一个活动 (1) 的半透明外观,对齐在屏幕 (4) 的顶部。
I have tried assigning these themes to activity number 2:
我尝试将这些主题分配给活动编号 2:
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/black</item>
</style>
<style name="CustomTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
But the result is always 3.
但结果总是 3。
If I set <item name="android:windowIsFloating">false</item>
in the CustomTheme
the result is 2.
如果我<item name="android:windowIsFloating">false</item>
在CustomTheme
结果中设置为 2。
Can anybody tell me how can I get 4? Thanks!
谁能告诉我怎样才能得到4?谢谢!
UPDATE:This is my activity 2 layout:
更新:这是我的活动 2 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#0000">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#FFFFFF">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu" android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
回答by Xavi Gil
Finally, this theme worked to get a result like image number 4:
最后,这个主题得到了像 4 号图像这样的结果:
<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:background">@android:color/transparent</item>
</style>
In my activity 2 layout, I could eihter set android:background="@android:color/transparent"
or not set any value at all to make it work.
在我的活动 2 布局中,我可以设置android:background="@android:color/transparent"
或不设置任何值以使其工作。
Thanks to MikeIsrael and Veer for their help.
感谢 MikeIsrael 和 Veer 的帮助。
回答by arm
I've read the other solutions, but here is my solution:
我已经阅读了其他解决方案,但这是我的解决方案:
style.xml
样式文件
<resources>
<style name="mytransparent.windowNoTitle" parent="android:Theme.Holo.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
<style name="mytransparent.windowTitle" parent="android:Theme.Holo.Light">
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
AndroidManifest.xml
AndroidManifest.xml
<activity
android:name=".LoginActivity"
android:theme="@style/mytransparent.windowTitle"
android:configChanges="orientation"
android:label="@string/title_activity_login"
android:screenOrientation="portrait" ></activity>
回答by Vadim Leb
If you use AppCompatActivity
then you should use as parent Theme.AppCompat
otherwise application can hang or crash with error (java.lang.RuntimeException: Unable to start activity ComponentInfo ... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.)
.
如果您使用AppCompatActivity
,则应将其用作父级,Theme.AppCompat
否则应用程序可能会因错误 ( java.lang.RuntimeException: Unable to start activity ComponentInfo ... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.)
.
Put this in your styles:
把它放在你的风格中:
<style name="MyTheme" parent="AppTheme.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowContentOverlay">@null</item>
</style>
Then add MyTheme to your activity manifest:
<activity android:name=".MyActivity" android:theme="@style/MyTheme">
然后将 MyTheme 添加到您的活动清单中:
<activity android:name=".MyActivity" android:theme="@style/MyTheme">
回答by Veer
First have the Transparent Theme activity:
首先有透明主题活动:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Assign the transparent theme to your activity in Manifest:
在 Manifest 中为您的活动分配透明主题:
<activity android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Transparent"/>
Create the layout as per your requirement and set the content view of your activity to that layout.
根据您的要求创建布局并将您的活动的内容视图设置为该布局。
Try following layout:
尝试以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_alignParentTop="true">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu" android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
Hope it is helpful.
希望它有帮助。
回答by MikeIsrael
I am not sure how to do it through the xml, but this should work programmatically, try adding this to your activity (maybe to the mainview of the activity).
我不确定如何通过 xml 来做到这一点,但这应该以编程方式工作,尝试将其添加到您的活动中(可能添加到活动的主视图中)。
//grab layout params
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
//remove the dim effect
lp.dimAmount = 0;