How do I create a transparent Activity on Android?

前端 未结 22 2123
忘掉有多难
忘掉有多难 2020-11-21 04:43

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

How can I achieve this?

相关标签:
22条回答
  • 2020-11-21 05:25

    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>
    
    0 讨论(0)
  • 2020-11-21 05:25

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

    android:theme="@android:style/Theme.Translucent"
    
    0 讨论(0)
  • 2020-11-21 05:27

    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.

    0 讨论(0)
  • 2020-11-21 05:27

    You can remove setContentView(R.layout.mLayout) from your activity and set theme as android:theme="@style/AppTheme.Transparent". Check this link for more details.

    0 讨论(0)
  • 2020-11-21 05:29

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

    0 讨论(0)
  • 2020-11-21 05:30

    All those answers might be confusing, there is a difference between Transparent activity and None UI activity.

    Using this:

    android:theme="@android:style/Theme.Translucent.NoTitleBar"

    Will make the activity transparent but will block the UI.

    If you want a None UI activity than use this:

    android:theme="@android:style/Theme.NoDisplay"

    0 讨论(0)
提交回复
热议问题