Jak utworzyć przezroczystą aktywność na Androidzie?

Chcę utworzyć przezroczystą aktywność na innej aktywności.

Jak mogę to osiągnąć?

Author: Peter Mortensen, 2010-02-01

18 answers

Dodaj następujący styl do pliku res/values/styles.xml (jeśli go nie masz, utwórz go.) Oto kompletny plik:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <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>
</resources>

(wartość @color/transparent jest wartością koloru #00000000, którą umieściłem w pliku res/values/color.xml. Możesz również użyć @android:color/transparent w późniejszych wersjach Androida.)

Następnie Zastosuj styl do swojej aktywności, na przykład:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
 1244
Author: gnobal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:23:56

Idzie tak:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
 180
Author: yanchenko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-01 14:47:54

Z biblioteką "AppCompat" lub "Android Design Support Library"jest trochę inaczej:

W stylu.xml:

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:background">#33000000</item> <!-- Or any transparency or 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>
W AndroidManifest.xml:
<activity>
    android:name=".WhateverNameOfTheActivityIs"
    android:theme="@style/Theme.AppCompat.Translucent"
    ...
</activity>
 86
Author: Andrey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-28 08:30:50

Zadeklaruj swoją aktywność w manifeście w następujący sposób:

 <activity   
     android:name=".yourActivity"    
     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>

I dodaj przezroczyste tło do układu.

 33
Author: Deepak Swami,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-27 00:51:45

Przypisz półprzezroczysty motyw do działania, które chcesz zrobić przezroczystym w pliku manifestu Androida twojego projektu:

<activity
    android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
 26
Author: Jigar Pandya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:31:24

Chciałem dodać do tego trochę, ponieważ jestem również nowym deweloperem Androida. Zaakceptowana odpowiedź jest świetna, ale wpadłem w kłopoty. Nie byłem pewien, jak dodać kolor do kolorów.plik xml. Oto jak należy to zrobić:

Colors=xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="class_zero_background">#7f040000</color>
     <color name="transparent">#00000000</color>
</resources>
W moich oryginalnych kolorach.plik xml miałem tag "drawable":
<drawable name="class_zero_background">#7f040000</drawable>

I zrobiłem to również dla koloru, ale nie zrozumiałem, że odniesienie "@color/ "oznaczało szukanie tagu" color " W XML. I pomyślałem, że powinienem wspomnieć o tym, aby pomóc innym.

 15
Author: Camille Sévigny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:25:30

Osiągnąłem to w 2.3.3, dodając android:theme="@android:style/Theme.Translucent" w znaczniku aktywności w manifeście.

Nie znam się na niższych wersjach...
 14
Author: androabhay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:26:07

W moim przypadku, muszę ustawić motyw na runtime w Javie na podstawie pewnych warunków. Więc stworzyłem jeden temat w stylu (podobny do innych odpowiedzi):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <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>
</resources>

Następnie w Javie zastosowałem go do mojej aktywności:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        String email = getIntent().getStringExtra(AppConstants.REGISTER_EMAIL_INTENT_KEY);
        if (email != null && !email.isEmpty())
        {
            // We have the valid email ID, no need to take it from user, prepare transparent activity just to perform bg tasks required for login
            setTheme(R.style.Theme_Transparent);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

        }
        else
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dummy);


    }

Pamiętaj o jednym ważnym punkcie tutaj: musisz wywołać setTheme() funkcję przed super.onCreate(savedInstanceState);. Przegapiłem ten punkt i stucked na 2 godziny, myśląc, Dlaczego mój temat nie jest odzwierciedlone w czasie uruchomienia.

 10
Author: Gem,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-13 09:18:25

W funkcji onCreate , poniżej setContentView, Dodaj tę linię:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
 8
Author: Leebeedev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:32:17

Pozwól, aby obraz tła aktywności był przezroczysty. Lub Dodaj motyw w pliku XML:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
 7
Author: jian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:26:40

Najprostszym sposobem, jaki znalazłem, jest ustawienie motywu aktywności w AndroidManifest na android:theme="@android:style/Theme.Holo.Dialog".

Następnie w metodzie aktywności OnCreate wywołaj getWindow().setBackgroundDrawable(new ColorDrawable(0));.

 6
Author: Drew Leonce,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:27:56

Do działania okna używam tego:

getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);

Ale musisz również ustawić główny widok w aktywności na niewidoczny. W przeciwnym razie tło będzie niewidoczne, podczas gdy wszystkie widoki w nim będą widoczne.

 4
Author: Pavel Kataykin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:37:12

Zrobiłem dwie rzeczy, dzięki czemu Moja aktywność stała się przejrzysta. Są poniżej.

  1. W pliku manifest właśnie dodałem poniższy kod w znacznikuactivity .

    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
    
  2. I ustawiłem tło głównego layoutu dla tej czynności jako "#80000000". Jak

    android:background="#80000000"
    
Dla mnie działa idealnie.
 3
Author: Md Sufi Khan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:29:45

Przypisać mu półprzezroczysty motyw

android:theme="@android:style/Theme.Translucent.NoTitleBar"
 2
Author: saurabh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-25 06:53:46

Uwaga 1: w folderze Drawable Utwórz test.xml i skopiuj następujący kod

   <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke android:width="2dp" />

    <gradient
        android:angle="90"
        android:endColor="#29000000"
        android:startColor="#29000000" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape>

// Uwaga: Narożniki i kształt są zgodne z Twoimi wymaganiami.

// Uwaga 2:Utwórz xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/test"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.09"
            android:gravity="center"
         android:background="@drawable/transperent_shape"
            android:orientation="vertical" >
     </LinearLayout>
    </LinearLayout>
 1
Author: ManiTeja,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-11 09:32:27

Są dwa sposoby:

  1. Używanie Motywu.NoDisplay
  2. Używanie Motywu.Przezroczyste.NoTitleBar

Używanie Theme.NoDisplay nadal będzie działać ... ale tylko na starszych urządzeniach z Androidem. Na Androidzie 6.0 i nowszych, za pomocą motywu.NoDisplay bez wywołania finish() w onCreate() (or, technically, before onResume()) będzie crash Twoja aplikacja. Dlatego zalecenie jest stosowanie Theme.Translucent.NoTitleBar, które nie cierpi z powodu tego ograniczenia."

 1
Author: Shubham Soni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-11 18:42:52

Wystarczy dodać następującą linię do znacznika aktywności w pliku manifestu, który musi wyglądać przezroczysto.

android:theme="@android:style/Theme.Translucent"
 0
Author: Kanagalingam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-28 22:35:44

Oprócz powyższych odpowiedzi:

Aby uniknąć awarii Androida Oreo związanej z aktywnością

<style name="AppTheme.Transparent" parent="@style/Theme.AppCompat.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

<activity
     android:name="xActivity"
     android:theme="@style/AppTheme.Transparent" />
 0
Author: Zafer Celaloglu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-26 09:55:01