Android实现沉浸式顶部

发布时间:2020-11-02 16:30:45 作者:Leah
来源:亿速云 阅读:142

Android实现沉浸式顶部?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

主页面代码:activity_main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"

  tools:context=".MainActivity">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:text="Hello World!"

    android:gravity="center"
    android:background="@color/colorAccent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <!-- android:fitsSystemWindows="true" android:clipToPadding="true"-->
</android.support.constraint.ConstraintLayout>

添加三个文件:三份 style 文件,即默认的values(不设置状态栏透明)、values-v19、values-v21(解决半透明遮罩问题)。

values 下 style.xml

<style name="TranslucentTheme" parent="AppTheme">
    <!--在Android 4.4之前的版本上运行,直接跟随系统主题-->

  </style>

values-v19 下 style.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<resources>
  <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
  </style>

</resources>

values-v21 下 style.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<resources>
  <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
  </style>

</resources>

这里需要在:AndroidMainfest.xml 里添加样式。

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="controller.hzl.com.dingbu2">

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
      android:theme="@style/TranslucentTheme"
      >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>

</manifest>

看完上述内容,你们掌握Android实现沉浸式顶部的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Android如何自定义ToolBar和实现沉浸式
  2. Android WebView实现顶部进度条

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi

上一篇:java数组与以逗号分隔开的字符串实现相互转换

下一篇:vue缓存之keep-alive的理解和应用详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》