Activity with transparent background

前端 未结 6 478
南旧
南旧 2020-12-09 18:33

what I want to achieve is activity with dialog-like transparency with 100% visibility of RelativeLayout content. This is activity\'s xml:



        
相关标签:
6条回答
  • 2020-12-09 18:46

    I added android:background="#c0000000" to LinearLayout. Now background is transparent as I wanted to, but also TextViews inside RelativeLayout are also transparent.. how to change it?

    Add a solid background to your RelativeLayout element. This way the RelativeLayout will have a solid background, and only the margins will be transparent.

    0 讨论(0)
  • 2020-12-09 18:48

    After setting LinearLayout transparency try to set it's child transparency to the value that you want. This should make them have different transparency than LinearLayout. Try it.

    0 讨论(0)
  • 2020-12-09 18:49

    Create Style

    <style name="MyTransparentTheme" parent="android:Theme.Dialog">
           <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>
    

    this is manifest:

    <activity
                android:name="your package.activity"
                android:theme="@style/MyTransparentTheme">
            </activity>
    
    0 讨论(0)
  • 2020-12-09 18:50

    If your activity needs to extends from AppCompatActivity, you can create a custom style for your activity

    <style name="transparentActivity" parent="Theme.AppCompat.NoActionBar">
        <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>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    

    And then change the default style of your activity in the Manifest

    <activity android:name=".MyActivity"
            android:theme="@style/transparentActivity"/>
    

    Otherwise, if you do not need your activity to extend from AppCompatActivity, just change the theme of your activity in the manifest in this way.

    <activity android:name=".MyActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
    
    0 讨论(0)
  • 2020-12-09 19:04

    Try to set

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    

    in your activity.

    You can also try to do this in a style:

     <style name="AppTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>
    
    0 讨论(0)
  • 2020-12-09 19:06

    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">@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 res/values/color.xml file. Later versions of Android define a transparent color for you, so with those versions you can use @android:color/transparent directly in the style and skip the color.xml step (note the additional android: in the definition))

    Then apply the style to your activity, for example:

    <activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
    ...
    </activity>
    

    Taken from here.

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