如何使活动的背景半透明?

4
我希望这个活动中的白色部分是半透明的,这样我就可以部分地看到下面的活动。 这个活动使用多个嵌套的LinearLayouts(即一个具有红色背景的LinearLayout位于具有白色背景的LinearLayout内部)。 我该如何实现这一点?

enter image description here

这是我的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.h8pathak.dash">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name="com.example.h8pathak.dash.app.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".activity.RegisterActivity"/>
        <activity android:name=".MainActivity"/>
        <activity android:name=".feed.NewsFeed"/>
        <activity android:name=".feed.NewPost"/>
        <activity android:name=".AnswerPost"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
            <!--android:theme="@android:style/Theme.Translucent.NoTitleBar"-->

    </application>

</manifest>

我正在处理的活动是 .AnswerPost。

首先,您没有告诉我们“白色部分”是什么。它是TextView、EditText、自定义View还是其他什么?无论如何,在Android上,任何视图都可以通过background属性设置为半透明背景。 - IgorGanapolsky
https://dev59.com/35Xfa4cB1Zd3GeqPg48R - Mangesh
2个回答

4

你可以借助以下步骤创建一个透明的活动:

1.在xml文件中使用以下代码将布局背景设置为透明:

android:background="@android:color/transparent"

2. 同时,在清单文件中为该特定活动设置透明主题。

   <activity
        android:name="Your activity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
    </activity>

java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.h8pathak.dash/com.example.h8pathak.dash.AnswerPost}: java.lang.IllegalStateException: 您需要使用一个 Theme.AppCompat 主题(或其子类)来运行此活动。我该怎么办? - TheHardRock
我现在已经分享了它。 - TheHardRock
为什么需要使用 NoTitleBar.Fullscreen?我认为这只与包含工具栏的活动有关... - IgorGanapolsky

3
您可以按照以下两个步骤操作:
1.在您的活动中
2.使用以下代码实现您的功能:
 requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));

    setContentView(R.layout.alarmring);
  1. In your manifest

    <activity
        android:name="com.calender.alarms.alarm_interfac_adapter_alarm"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent" >
    </activity>
    

如果您需要任何帮助,请告诉我。如果这有所帮助,请标记一下。


这个完美地运作了。非常简洁明了的代码。谢谢! - IgorGanapolsky

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