Android沉浸式以及解决虚拟键问题



styles 

 <style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

styles-v19

 <style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
    </style>

styles-v21

<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

设置app theme:

<application
        android:name=".utils.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/ImageTranslucentTheme">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中实现沉浸,需要通过以下几个步骤: 1. 在主题样中设置全屏和透明状态栏: ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowFullscreen">true</item> <item name="android:windowTranslucentStatus">true</item> </style> ``` 2. 在布局文件中设置顶部布局的间距: ```xml <RelativeLayout android:id="@+id/rl_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/status_bar_height" android:background="@color/colorPrimary"> <!--顶部布局内容--> </RelativeLayout> ``` 其中,`@dimen/status_bar_height`为状态栏高度,需要在`dimens.xml`文件中定义。 3. 在活动中设置状态栏颜色: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); } ``` 4. 在代码中设置全屏和隐藏虚拟导航栏: ```java //设置全屏 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); //隐藏虚拟导航栏 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); ``` 以上就是Android中实现沉浸的基本步骤。需要注意的是,沉浸并不是适用于所有场景的,需要根据具体情况进行选择。同时,如果在Android 4.4及以下版本中使用沉浸,需要使用第三方库进行支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值