本问题已经有最佳答案,请猛点这里访问。
以下示例显示了在Android上使活动透明的简单方法。
[参考:http://www.coderzheaven.com/2011/07/20/how-to-create-a-transparent-activity-in-android/]
我的活动:
public class TranslucentActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
现在主要的布局文件main.xml:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@drawable/red"
android:textStyle="bold"
/>
android:text="This is Translucent Activity Demo From CoderzHeaven"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/red"
android:textStyle="bold">
好的,现在这里是必须应用于使活动透明的样式。在res/values文件夹中创建名为style.xml的文件,并将此代码复制到其中:
true
@android:color/transparent
@null
true
true
false
现在应该在哪里应用这个主题?它应用于AndroidManifest.xml
该文件如下所示。
package="pack.coderzheaven"
android:versionCode="1"
android:versionName="1.0">
android:theme="@style/Theme.Transparent"
android:label="@string/app_name">
检查应用此样式的清单中的此行。
机器人:主题=" @风格/ Theme.Transparent"
appname和不同的颜色位于strings.xml中
TranslucentActivity! From Coderzheaven
TranslucentActivity
#ffffff
#000000
#2554C7
#347C2C
#ff9900
#FF00FF
#a020f0
#778899
#C11B17
#FFFF8C
#b0e0e6
#2F1700
#7D2252
Select Category
#606060
输出:抛出异常:java.lang.IllegalStateException:您需要将Theme.AppCompat主题(或后代)与此活动一起使用。
有什么建议吗?有解决方案吗我该如何解决这个问题?
但我的目标布局是透明的。
我得到了解决方案
如何在Android上创建透明活动?
With the"AppCompat" library or"Android Design Support Library" it's a bit different:
In the styles.xml:
#33000000
true
@android:color/transparent
@null
true
@android:style/Animation
In the AndroidManifest.xml:
android:name=".WhateverNameOfTheActivityIs"
android:theme="@style/Theme.AppCompat.Translucent"
...
只需将样式Theme.Transparent的父主题更改为Theme.AppCompat即可
将扩展活动更改为AppCompatActivity
使用AppCompatActivity扩展您的活动
public class TranslucentActivity extends AppCompatActivity