Android 设置透明 Activity

App中窗口型的页面,我们可以使用Dialog,popwindow,另外还能透明的Activity来实现。

# 当然这也简单:在 AndroidManifest.xml 中对 目标 Activity 设置样式为透明 就行:

<application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

      // 目标 Activity
      <activity
            android:name=".ui.Activity"
            android:theme="@style/TranslucentTheme" />

      ...

</application>

样式 style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
<style name="TranslucentTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/transparent</item>
    </style>

但是运行报错了: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

告诉我需要使用样式 Theme.AppCompat theme ,所以我做了如下的调整就解决了。

// 目标 Activity
      <activity
            android:name=".ui.Activity"
            android:theme="@style/AppTheme.TranslucentTheme" />


<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


   <!-- 目标 Activity 透明样式 -->
    <style name="AppTheme.TranslucentTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/transparent</item>
    </style>

## 因为我项目中的Activity是继承的AppCompatActivity,所以贴一下 AppCompatActivity 源码(只贴了开头注释就会明白):

/**
 * Base class for activities that wish to use some of the newer platform features on older
 * Android devices. Some of these backported features include:
 *
 * <ul>
 *     <li>Using the action bar, including action items, navigation modes and more with
 *     the {@link #setSupportActionBar(Toolbar)} API.</li>
 *     <li>Built-in switching between light and dark themes by using the
 *     {@link androidx.appcompat.R.style#Theme_AppCompat_DayNight Theme.AppCompat.DayNight} theme
 *     and {@link AppCompatDelegate#setDefaultNightMode(int)} API.</li>
 *     <li>Integration with <code>DrawerLayout</code> by using the
 *     {@link #getDrawerToggleDelegate()} API.</li>
 * </ul>
 *
 * <p>Note that every activity that extends this class has to be themed with
 * {@link androidx.appcompat.R.style#Theme_AppCompat Theme.AppCompat} or a theme that extends
 * that theme.</p>
 *
 * <div class="special reference">
 * <h3>Developer Guides</h3>
 *
 * <p>For information about how to use the action bar, including how to add action items, navigation
 * modes and more, read the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action
 * Bar</a> API guide.</p>
 * </div>
 */
public class AppCompatActivity extends FragmentActivity implements AppCompatCallback,
        TaskStackBuilder.SupportParentable, ActionBarDrawerToggle.DelegateProvider {
    ...
}

主要看下上面对于样式(theme)的描述:

 *   Note that every activity that extends this class has to be themed with
 * {@link androidx.appcompat.R.style#Theme_AppCompat Theme.AppCompat} or a theme that extends
 * that theme.

简单翻译即:请注意,扩展此类的每个活动都必须使用{@link androidx.appcompat.R.style#Theme_AppCompat Theme.AppCompat}或扩展该主题的主题作为主题。

⚠️ 所以报这个错的原因是我的 activity 继承了 AppCompatActivity,它来自androidx.appcompat.app.AppCompatActivity,所以必须使用{@link androidx.appcompat.R.style#Theme_AppCompat Theme.AppCompat}或扩展该主题的主题作为主题(需要设置兼容的样式)。

其实设置透明 Activity还是简单的设置样式,只不过需要注意这点!当然也能在代码里设置:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setTheme(R.style.TranslucentTheme)
        init()
    }

⚠️  1. R.style.TranslucentTheme 需要兼容上面提到的注意点!!

⚠️  2. 这样的命名方式:<style name="AppTheme.TranslucentTheme">  即 父Style的名字作为前缀,然后通过“.”连接新定义Style,相当于 TranslucentTheme 继承与AppTheme,是一种命名方式,这种不适应在代码直接调用!

 

### 这里说到 透明Activity, 需要稍微注意一下 透明Activity生命周期:

 

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中实现透明Activity的点击穿透,可以通过以下步骤进行操作: 1. 在Manifest文件中声明透明Activity: ```xml <activity android:name=".TransparentActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"> </activity> ``` 2. 创建TransparentActivity类并继承自Activity,并在onCreate方法中设置Activity透明属性: ```java public class TransparentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置Activity透明 getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH); // 设置布局等其他操作 setContentView(R.layout.activity_transparent); } } ``` 3. 在TransparentActivity的布局文件中,可以添加其他View元素,以便显示其他内容。 ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a transparent activity" android:textSize="24sp" android:layout_centerInParent="true"/> <!--其他View元素--> </RelativeLayout> ``` 通过以上步骤,我们可以创建一个透明Activity,并且该Activity不会拦截点击事件,点击事件会传递到下方的View,从而实现了透明Activity的点击穿透效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值