在Android 2.3.3或4.2上编程设置半透明主题时无法正常工作

7

我有两个活动:

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 public void click(View view) {
  Intent intent= new Intent(this, TranslucentActivity.class);
  startActivity(intent);        
 }
}

public class TranslucentActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  this.setTheme(android.R.style.Theme_Translucent);
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_translucent);
 }
}

布局 activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="click" />
</RelativeLayout>

布局活动透明

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TranslucentActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Hello_world" />
</RelativeLayout>

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.translucencytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.translucencytest.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.translucencytest.TranslucentActivity">
        </activity>
    </application>
</manifest>

当我在MainActivity中点击按钮时,它会启动具有透明背景的TranslucentActivity。在Android模拟器上的平台4.0.3上看起来很好,但在其他平台上(例如2.3.3或4.2),我得到的是黑色背景而不是透明背景。可能出了什么问题?
注:我不想使用清单声明活动的主题。

我刚刚测试了主题,它在不同的Android版本上按设计工作。 你的示例可能有很多问题。 半透明主题将显示后面的窗口,因此如果该窗口是黑色的,则看不到透明度。当您不为应用程序声明主题时,它不会透明,因为Android在调用setTheme之前使用默认主题,然后您将看到默认主题而不是主屏幕。为了能够帮助您,您至少应发布示例中使用的两个布局和清单。 - Emanuel Moecklin
@EmanuelMoecklin 我已经在我的帖子中添加了布局和清单。我怀疑默认主题被显示而不是半透明,但为什么它在4.0.3上运行而在2.3.3和4.2上不运行呢? - Wytas
你正在使用自己的@style/AppTheme,所以你也需要将它发布出来,这样我才能够测试你的示例。不同的Android版本对于相同的屏幕元素(视图)使用不同的主题、样式和资源。制造商还会为他们的设备设置主题,以使其与竞争设备有所区别。例如,在某个设备上,默认背景可能是黑色,而在另一个设备上可能是略带透明度并带有渐变效果。除非你自己设置主题,否则你不能真正依赖于“事物”在每个设备上看起来都一样。 - Emanuel Moecklin
@EmanuelMoecklin 你可以在清单文件中删除 @style/AppTheme 这一行。我尝试过没有它,结果是一样的 - 它只能在4.0.3上工作。 - Wytas
如果您认为我的回答解决了您的问题,请将其标记为正确答案。 - Emanuel Moecklin
我本来不想使用清单文件,但现在看来我应该重新考虑我的设计了。良好的回答,Emanuel。 - Wytas
2个回答

11

setTheme()方法在使用上存在很多讨论,包括为什么它不能像预期的那样工作:

这些讨论的要点是,尽管可以在代码中使用setTheme方法来设置主题,但在清单文件中设置主题会更加可靠(即使Dianne Hackborn也推荐使用清单方式而不是setTheme方法,详见上面的第三个链接)。

特别是对于定义背景的情况,你在模拟器或图形布局编辑器中看到的结果可能无法完全在实际设备上呈现。所以你在4.0.3模拟器中看到的效果可能与实际设备上的不同(正如你已经注意到的那样 ;-).

如果没有特别的原因需要使用setTheme方法来设置Activity的主题,我建议你按以下方式更改清单文件:

<activity android:name=".TranslucentActivity"
          android:theme="@android:style/Theme.Translucent"/>

您仍然可以使用setTheme来为布局的其他元素设置主题,但是我在创建透明或对话框式活动方面没有找到其他可行的解决方案。


有趣而又奇怪。我发现如果在清单文件中将windowBackground设置为透明,有时它会起作用,有时则不会。但是,如果你只是在清单文件中使用@android:style/Theme.Translucent,它似乎总是有效的。 - Fattie

-1
<activity android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:name="com.example.translucencytest.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:name="com.example.translucencytest.TranslucentActivity">
</activity>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接