我有两个活动:
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),我得到的是黑色背景而不是透明背景。可能出了什么问题?
注:我不想使用清单声明活动的主题。
@style/AppTheme
这一行。我尝试过没有它,结果是一样的 - 它只能在4.0.3上工作。 - Wytas