在Android 2.3.3或4.2编程设置时,半透明的主题不工作主题、工作、Android

2023-09-12 23:43:40 作者:仙女味的糖果

我有两个活动:

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

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

布局activity_translucent

Layout activity_translucent

<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透明背景。所有看起来不错的Andr​​oid模拟器上平台4.0.3但在其他平台,如2.3.3或4.2,我得到的黑色背景,而不是透明的。什么地方出错了?

When I click button in MainActivity it starts TranslucentActivity with transparent background. All looks fine in android emulator on platform 4.0.3 But on other platforms, e.g. 2.3.3 or 4.2, I get black background instead of transparent. What could be wrong ?

P.S。我不希望使用清单申报的主题活动。

P.S. I do not want to use manifest to declare theme for activities.

推荐答案

有一个关于setTheme(大量的讨论),或者说为什么setTheme()不按预期工作:

There's a lot of discussion on setTheme() or rather why setTheme() isn't working as expected:

https://$c$c.google.com/p/android/issues/detail?id=4394 https://$c$c.google.com/p/android/issues/detail?id=3793 https://groups.google.com/forum/?fromgroups=#!topic/android-developers/vSZHsVWUCqk 为什么getApplicationContext()。setTheme()在活动不起作用? https://code.google.com/p/android/issues/detail?id=4394 https://code.google.com/p/android/issues/detail?id=3793 https://groups.google.com/forum/?fromgroups=#!topic/android-developers/vSZHsVWUCqk Why getApplicationContext().setTheme() in a Activity does not work?

精华于讨论的是,setTheme不能很好地工作,同时设置主题清单做(甚至戴安娜Hackborn建议使用清单的方式在setTheme()方法,见上面第三个链接)。 这一点尤其是当它涉及到确定的背景。 结果你在模拟器或图形布局编辑器获得可不幸的是没有被转移到现实世界(讲实际的设备)。所以,你在你的4.0.3模拟器看可能不是一个真正的设备在相同的(因为你已经注意到了, - )。

Quintessential to that discussion is that the setTheme doesn't work well while setting the theme in the manifest does (even Dianne Hackborn recommends to use the manifest way over the setTheme() way, see third link above). This is especially true when it comes to defining the background. Results you get in the emulator or in the graphical layout editor can unfortunately not be transferred to the real world (speak actual devices). So what you see in your 4.0.3 emulator might not be the same on a real device (as you already noticed ;-).

如果没有使用setTheme为主题的活动,那么我会建议改变你的表现像这样具体的原因:

If there's no specific reason to use setTheme to theme your Activity then I would recommend to change your manifest like this:

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

您仍然可以使用setTheme来布局的主题等元素,但我还没有发现任何其他工作的解决方案,当谈到创建透明或对话框类的活动。

You can still use the setTheme to theme other elements of your layout but I haven't found any other working solution when it comes to creating transparent or dialog like activities.