Java 如何在Android 6.0(棉花糖)上创建透明布局?

Java 如何在Android 6.0(棉花糖)上创建透明布局?,java,android,eclipse,android-layout,android-studio,Java,Android,Eclipse,Android Layout,Android Studio,下面的示例显示了一个简单的方法,可以使活动在Android上透明 [参考: 我的活动: public class TranslucentActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

下面的示例显示了一个简单的方法,可以使活动在Android上透明

[参考:

我的活动:

public class TranslucentActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
现在,主布局文件main.xml:

现在,这个主题应该应用在哪里呢?它应用在AndroidManifest.xml中

该文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TranslucentActivity"
                  android:theme="@style/Theme.Transparent"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
在应用此样式的清单中选中此行

android:theme=“@style/theme.Transparent” appname和不同的颜色在strings.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">TranslucentActivity! From Coderzheaven</string>
    <string name="app_name">TranslucentActivity</string>
    <drawable name="white">#ffffff</drawable>
    <drawable name="black">#000000</drawable>
    <drawable name="blue">#2554C7</drawable>
    <drawable name="green">#347C2C</drawable>
    <drawable name="orange">#ff9900</drawable>
    <drawable name="pink">#FF00FF</drawable>
    <drawable name="violet">#a020f0</drawable>
    <drawable name="grey">#778899</drawable>
    <drawable name="red">#C11B17</drawable>
    <drawable name="yellow">#FFFF8C</drawable>
    <drawable name="PowderBlue">#b0e0e6</drawable>
    <drawable name="brown">#2F1700</drawable>
    <drawable name="Hotpink">#7D2252</drawable>
    <string name="select_Category">Select Category</string>
    <drawable name="darkgrey">#606060</drawable>
</resources>
输出:抛出异常:java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题或子代

有什么建议吗?有什么解决办法吗?我该怎么解决这个问题?。
但是我的目标布局是透明的。

只需更改样式主题的父主题。对主题透明。AppCompat

使用AppCompatActivity扩展您的活动

public class TranslucentActivity extends AppCompatActivity

将扩展活动更改为AppCompatActivity

我从中获得了解决方案

对于AppCompat库或Android设计支持库,它有点不同:

在styles.xml中:

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:background">#33000000</item> <!-- Or any 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>
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:background">#33000000</item> <!-- Or any 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>
<activity>
    android:name=".WhateverNameOfTheActivityIs"
    android:theme="@style/Theme.AppCompat.Translucent"
    ...
</activity>