【问题标题】:How to make the background of an activity translucent?如何使活动的背景半透明?
【发布时间】:2016-03-14 06:09:55
【问题描述】:

我希望此活动中的白色部分是半透明的,以便我可以部分地看到下面的活动。此活动使用多个嵌套的线性布局(即,一个具有红色背景的线性布局位于具有白色背景的线性布局内)。我怎样才能做到这一点?

这是我的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 还是什么?在任何情况下,任何 View 都可以通过 background 属性在 Android 上具有半透明背景。

标签: android android-layout android-activity


【解决方案1】:

你可以在

的帮助下创建一个透明的Activity

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 主题(或后代)与此活动。我该怎么办?
  • 我已经分享过了。
  • 为什么需要 NoTitleBar.Fullscreen?我认为这仅与包含工具栏的活动有关...
【解决方案2】:

您可以按照以下两个步骤进行

1.在你的活动中

 requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));

    setContentView(R.layout.alarmring);
  1. 在您的清单中

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

如果您需要任何帮助,请告诉我。如果有帮助,请标记它..

【讨论】:

  • 这非常有效。非常简洁的代码。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 2011-06-11
  • 2015-11-12
  • 2011-09-15
相关资源
最近更新 更多