Подтвердить что ты не робот

Прозрачная активность, заполняющая весь экран

Я хотел бы иметь активность (2) с полупрозрачным аспектом над другой деятельностью (1), выровненную в верхней части экрана (4).

enter image description here

Я попытался присвоить эти темы активности номеру 2:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/black</item>
</style>  

<style name="CustomTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
</style>    

Но результат всегда равен 3.

Если я устанавливаю <item name="android:windowIsFloating">false</item> в CustomTheme, результат равен 2.

Может ли кто-нибудь сказать мне, как я могу получить 4? Спасибо!

ОБНОВЛЕНИЕ: Это макет моей активности 2:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:background="#0000">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#FFFFFF">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</RelativeLayout>
4b9b3361

Ответ 1

Наконец, эта тема работала, чтобы получить результат, например, номер изображения 4:

  <style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:background">@android:color/transparent</item>
 </style>  

В макете моей деятельности 2 я мог установить android:background="@android:color/transparent" или вообще не устанавливать какое-либо значение, чтобы заставить его работать.

Благодаря MikeIsrael и Veer за их помощь.

Ответ 2

Я читал другие решения, но вот мое решение:

style.xml

<resources>
<style name="mytransparent.windowNoTitle" parent="android:Theme.Holo.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</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>
<style name="mytransparent.windowTitle" parent="android:Theme.Holo.Light">
    <item name="android:background">@android:color/transparent</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=".LoginActivity"
    android:theme="@style/mytransparent.windowTitle"
    android:configChanges="orientation"
    android:label="@string/title_activity_login"
    android:screenOrientation="portrait" ></activity>

Ответ 3

Если вы используете AppCompatActivity вы должны использовать в качестве родительского Theme.AppCompat иначе приложение может зависнуть или аварийно Theme.AppCompat с ошибкой (java.lang.RuntimeException: Unable to start activity ComponentInfo... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.).

Поместите это в свои стили:

<style name="MyTheme" parent="AppTheme.NoActionBar">
   <item name="android:windowIsTranslucent">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:backgroundDimEnabled">false</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowActionBar">false</item>
   <item name="android:windowFullscreen">false</item>
   <item name="android:windowContentOverlay">@null</item>
</style>

Затем добавьте MyTheme в манифест своей активности: <activity android:name=".MyActivity" android:theme="@style/MyTheme">

Ответ 4

Сначала у вас есть активность Transparent Theme:

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Назначьте прозрачную тему своей деятельности в манифесте:

    <activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:theme="@style/Theme.Transparent"/>

Создайте макет в соответствии с вашим требованием и настройте представление содержимого вашей деятельности на этот макет.

Попробуйте следующую компоновку:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_alignParentTop="true">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</RelativeLayout>

Надеюсь, что это будет полезно.

Ответ 5

Я не уверен, как это сделать с помощью xml, но это должно работать программно, попробуйте добавить это к вашей деятельности (возможно, к основному виду деятельности).

//grab layout params
WindowManager.LayoutParams lp = this.getWindow().getAttributes();

//remove the dim effect
lp.dimAmount = 0;