我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
当前回答
我在2.3.3上通过添加android:theme="@android:style/ theme来实现它。半透明的”在活动标签在清单。
我不知道更低的版本……
其他回答
注1:在Drawable文件夹中创建test.xml并复制以下代码
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" />
<gradient
android:angle="90"
android:endColor="#29000000"
android:startColor="#29000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
//注:角和形状按您的要求来定。
//注释2:创建xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/test"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09"
android:gravity="center"
android:background="@drawable/transperent_shape"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
对于对话活动,我使用这个:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
但是你也需要将活动中的主视图设置为不可见。否则,背景将是不可见的,而其中的所有视图将是可见的。
其他解决方案对我有用,但我注意到一个问题,可能会影响一些人。当我像这样开始我的活动时:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
而不是像这样的一行字
startActivity(new Intent(this, MainActivity.class));
我只得到黑色的背景,这很痛苦。我还没有弄清楚是什么导致了这个,我猜这是设备相关的(在小米上测试)。传递值也需要像这样。
startActivity(new Intent(this, MainActivity.class)
.putExtra("SOME_VALUE", value)
.putExtra("ANOTHER_VALUE", value2));
编辑: 将父样式更改为Theme.AppCompat.Light.NoActionBar似乎已经解决了这个问题
在styles.xml中:
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item> <!-- Or any transparency or color you need -->
<item name="android:windowNoTitle">true</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中:
<activity
android:name=".WhateverNameOfTheActivityIs"
android:theme="@style/Theme.AppCompat.Translucent">
...
</activity>
在我的情况下,我必须根据一些条件在java运行时设置主题。所以我在风格上创建了一个主题(类似于其他答案):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</resources>
然后在Java中,我把它应用到我的活动中:
@Override
protected void onCreate(Bundle savedInstanceState) {
String email = getIntent().getStringExtra(AppConstants.REGISTER_EMAIL_INTENT_KEY);
if (email != null && !email.isEmpty()) {
// We have the valid email ID, no need to take it from user,
// prepare transparent activity just to perform bg tasks required for login
setTheme(R.style.Theme_Transparent);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
} else
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
}
记住一个重要的一点:你必须在super.onCreate(savedInstanceState);;之前调用setTheme()函数。我错过了这一点,卡了2个小时,思考为什么我的主题没有在运行时反映出来。