App中窗口型的页面,我们可以使用Dialog,popwindow,另外还能透明的Activity来实现。
# 当然这也简单:在 中对 目标 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="."
android:theme="@style/TranslucentTheme" />
...
</application>
样式 :
<style name="AppTheme" parent="">
<!-- 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>
但是运行报错了: : You need to use a theme (or descendant) with this activity.
告诉我需要使用样式 theme ,所以我做了如下的调整就解决了。
// 目标 Activity
<activity
android:name="."
android:theme="@style/" />
<!-- Base application theme. -->
<style name="AppTheme" parent="">
<!-- 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="">
<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 #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 #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/">Action
* Bar</a> API guide.</p>
* </div>
*/
public class AppCompatActivity extends FragmentActivity implements AppCompatCallback,
, {
...
}
主要看下上面对于样式(theme)的描述:
* Note that every activity that extends this class has to be themed with
* {@link #Theme_AppCompat } or a theme that extends
* that theme.
简单翻译即:请注意,扩展此类的每个活动都必须使用{@link #Theme_AppCompat }或扩展该主题的主题作为主题。
⚠️ 所以报这个错的原因是我的 activity 继承了 AppCompatActivity,它来自,所以必须使用{@link #Theme_AppCompat }或扩展该主题的主题作为主题(需要设置兼容的样式)。
其实设置透明 Activity还是简单的设置样式,只不过需要注意这点!当然也能在代码里设置:
override fun onCreate(savedInstanceState: Bundle?) {
(savedInstanceState)
setTheme()
init()
}
⚠️ 1. 需要兼容上面提到的注意点!!
⚠️ 2. 这样的命名方式:<style name=""> 即 父Style的名字作为前缀,然后通过“.”连接新定义Style,相当于 TranslucentTheme 继承与AppTheme,是一种命名方式,这种不适应在代码直接调用!
### 这里说到 透明Activity, 需要稍微注意一下 透明Activity生命周期: