csdnceshi79
python小菜
2010-02-01 13:28
采纳率: 0%
浏览 470

如何在 Android 上创建一个透明的活动?

I want to create a transparent Activity on top of another activity.

How can I achieve this?

转载于:https://stackoverflow.com/questions/2176922/how-do-i-create-a-transparent-activity-on-android

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

18条回答 默认 最新

  • csdnceshi61
    derek5. 2010-04-23 17:37
    已采纳

    Add the following style in your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:

    <?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>
    

    (The value @color/transparent is the color value #00000000 which I put in the res/values/color.xml file. You can also use @android:color/transparent in later Android versions.)

    Then apply the style to your activity, for example:

    <activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
    ...
    </activity>
    
    点赞 评论
  • csdnceshi70
    笑故挽风 2010-02-01 14:47

    It goes like this:

    <activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    
    点赞 评论
  • csdnceshi69
    YaoRaoLov 2011-04-22 12:55

    I wanted to add to this a little bit as I am a new Android developer as well. The accepted answer is great, but I did run into some trouble. I wasn't sure how to add in the color to the colors.xml file. Here is how it should be done:

    colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
         <color name="class_zero_background">#7f040000</color>
         <color name="transparent">#00000000</color>
    </resources>
    

    In my original colors.xml file I had the tag "drawable":

    <drawable name="class_zero_background">#7f040000</drawable>
    

    And so I did that for the color as well, but I didn't understand that the "@color/" reference meant look for the tag "color" in the XML. I thought that I should mention this as well to help anyone else out.

    点赞 评论
  • csdnceshi56
    lrony* 2011-09-21 12:07

    I achieved it on 2.3.3 by just adding android:theme="@android:style/Theme.Translucent" in the activity tag in the manifest.

    I don't know about lower versions...

    点赞 评论
  • csdnceshi50
    三生石@ 2012-02-19 14:12

    Just let the activity background image be transparent. Or add the theme in the XML file:

    <activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    
    点赞 评论
  • csdnceshi69
    YaoRaoLov 2012-07-02 05:07

    Declare your activity in the manifest like this:

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

    And add a transparent background to your layout.

    点赞 评论
  • csdnceshi52
    妄徒之命 2013-06-12 10:54

    The easiest way that I have found is to set the activity's theme in the AndroidManifest to android:theme="@android:style/Theme.Holo.Dialog".

    Then in the activity's onCreate method, call getWindow().setBackgroundDrawable(new ColorDrawable(0));.

    点赞 评论
  • csdnceshi56
    lrony* 2013-07-25 06:31

    Assign it the Translucent theme

    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    
    点赞 评论
  • csdnceshi60
    ℡Wang Yan 2013-09-02 07:06

    I just did two things, and it made my activity transparent. They are below.

    1. In the manifest file I just added the below code in the activity tag.

      android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
      
    2. And I just set the background of the main layout for that activity as "#80000000". Like

      android:background="#80000000"
      

    It perfectly works for me.

    点赞 评论
  • csdnceshi74
    7*4 2013-10-29 05:36

    Assign the translucent theme to the activity that you want to make transparent in the Android manifest file of your project:

    <activity
        android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    
    点赞 评论
  • weixin_41568184
    叼花硬汉 2014-10-14 20:18

    In the onCreate function, below the setContentView, add this line:

    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    
    点赞 评论
  • csdnceshi74
    7*4 2015-07-28 21:26

    With the "AppCompat" library or "Android Design Support Library" it's a bit different:

    In the styles.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>
    

    In the AndroidManifest.xml:

    <activity>
        android:name=".WhateverNameOfTheActivityIs"
        android:theme="@style/Theme.AppCompat.Translucent"
        ...
    </activity>
    
    点赞 评论
  • csdnceshi62
    csdnceshi62 2015-10-26 14:15

    In my case, i have to set the theme on the runtime in java based on some conditions. So I created one theme in style (similar to other answers):

    <?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>
    

    Then in Java I applied it to my activity:

    @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);
    
    
        }
    

    Remember one Important point here: You must call the setTheme() function before super.onCreate(savedInstanceState);. I missed this point and stucked for 2 hours, thinking why my theme is not reflected at run time.

    点赞 评论
  • csdnceshi52
    妄徒之命 2015-12-11 09:32

    Note 1:In Drawable folder create test.xml and copy the following code

       <?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>
    

    // Note: Corners and shape is as per your requirement.

    // Note 2:Create 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>
    
    点赞 评论
  • csdnceshi58
    Didn"t forge 2016-08-16 14:21

    Just add the following line to the activity tag in your manifest file that needs to look transparent.

    android:theme="@android:style/Theme.Translucent"
    
    点赞 评论
  • csdnceshi74
    7*4 2017-04-25 17:14

    For dialog activity I use this:

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

    But you also need to set your main View in the activity to invisible. Otherwise the background will be invisible while all views in it will be visible.

    点赞 评论
  • csdnceshi66

    There're two ways:

    1. Using Theme.NoDisplay
    2. Using Theme.Translucent.NoTitleBar

    Using Theme.NoDisplay will still work… but only on older Android devices. On Android 6.0 and higher, using Theme.NoDisplay without calling finish() in onCreate() (or, technically, before onResume()) will crash your app. This is why the recommendation is to use Theme.Translucent.NoTitleBar, which does not suffer from this limitation.”

    点赞 评论
  • csdnceshi74
    7*4 2018-09-26 09:55

    in addition to the above answers:

    to avoid android Oreo related crash on activity

    <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" />
    
    点赞 评论

相关推荐