代码之家  ›  专栏  ›  技术社区  ›  Prashant

如何获取Android活动的半透明背景?

  •  -2
  • Prashant  · 技术社区  · 6 年前

    我想要一个半透明的背景为我的活动,但内容的活动,如按钮和图像应该是正常可见的。

    我正在使用minsdkversion 17,我的活动扩展了appcompatactivity。

    我应该使用哪种样式特性来实现我的目标?

    2 回复  |  直到 6 年前
        1
  •  1
  •   tushar    6 年前
    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>
    
        2
  •  0
  •   Raj    6 年前

    您可以将活动背景更改为-

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:background="#27ffffff"/>