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

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

<applicationandroid: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<activityandroid: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<activityandroid: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生命周期:

Android 设置透明 Activity相关推荐

  1. Android设置透明状态栏,仿ios状态栏

    为什么80%的码农都做不了架构师?>>>    Android设置透明状态栏,仿ios状态栏 设置透明状态栏后,效果如下: 我的实现思路是: 在根布局上添加一块布局 添加了一块线性布 ...

  2. Android设置窗体Activity背景透明

    为什么80%的码农都做不了架构师?>>>    背景透明 style.xml <item name="android:windowBackground"&g ...

  3. Android设置透明状态栏以及隐藏状态栏

    在很多情况下,我们都需要让项目的某个界面状态栏设置为透明或者直接隐藏起来,这样使我们的界面看起来整体美观大方一些.下面来给大家演示一下如何将Android状态栏设置为透明或者将状态栏隐藏起来. 我个人 ...

  4. android设置透明状态栏

    在写这篇文章之前也看过很多大牛的博客,但是大多数都写的比较深奥和跳跃 而且网上还有很多对于透明状态栏及沉浸式状态栏的争论,简直看的头晕眼花 在此我用专业菜鸟的术语给大家解释一下: 沉浸式状态栏:就是你 ...

  5. Android 设置透明的方法

    第一种 也是自己经常使用的 就是在颜色后面添加透明度 比如ui给的交互图 不透明度为60% 我们写的使用直接在颜色值前面添加 android:textColor="#60FFFFFF&quo ...

  6. Android设置透明效果的三种方法(转)

    1.使用Android系统自带的透明效果资源  <Button  android:background="@android:color/transparent"/>   ...

  7. Qt for ios / Qt for Android 设置透明状态栏

    前言 所谓的透明状态栏,有些人也称之为沉浸式标题栏,就是要让系统的通知栏也显示出来,这样整体看上起会非常美观,目前用到的所有软件几乎都是这种风格,如果是用原生开发的话其实是非常容易的,但是如果是跨平台 ...

  8. android设置透明主题后背景为黑色,android – 设置主题Programmactically导致黑色背景...

    当我运行我的 Android应用程序时,我正在调用一种方法来检查应用程序是否在平板电脑上运行: public boolean isTablet(Context context){ boolean xl ...

  9. Android 优化 透明Activity展示loading关闭时闪烁

    1. 背景 之前设计了一个支付SDK,因为需要展示loading以及支付页面,也为了不需要用户传入Activity引用,设计了一个透明得Activity,但是发现在实践得时候,关闭这个Activity ...

最新文章

  1. C# 中的Async 和 Await 的用法详解
  2. Python主要智能优化算法库汇总
  3. java网络编程udp_Java网络编程之UDP
  4. Android 性能优化---(8)APP启动时间优化指南
  5. Android开发笔记(一百四十四)高仿支付宝的头部伸缩动画
  6. mysql执行计划id相同_MySQL|MySQL执行计划
  7. python库-密码学库pynacl
  8. 视频教程-PHP之socket入门实战websocket聊天室-PHP
  9. R语言常用数据文件的导入
  10. Node2Vec实战
  11. VSCode 下载速度慢问题解决
  12. 大数据数据库(HBase)
  13. Typora 安装包2021年11月最后一次免费版本的安装包下载V13.6.1
  14. android调用webservice,Android开发调用WebService的方法示例
  15. 工作之余可以回味的经典
  16. Java Lambda reduce 例子 全网reduce最清晰易懂的例子,不服来辩 xxx XXX
  17. Reporting verbs
  18. 华为FreeBuds SE耳机突然没有声音了是怎么回事?
  19. emmc和SPI共舞
  20. 什么是单例模式 (Singleten)

热门文章

  1. linux学习笔记--host命令
  2. html中js隐藏div的高度,jQuery怎么获取隐藏元素的高度?
  3. NFT熊市大考:并购整合进行时
  4. 说话人验证论文翻译:Generalized end-to-end loss for speaker verification
  5. 新标韩语第一册 单词-句子
  6. tcpdump命令详解(整理)
  7. pyecharts 进阶之地图+涟漪散点图+路径图(六)
  8. freeswitch SIP内呼与外呼配置
  9. Pycharm调试错误:连接 Python 调试器失败 Socket closed
  10. EXCEL 2007施工进度横道图制作步骤及实战练习