Skip to content

Commit

Permalink
功能实现:Android 6.0.1 上沉侵式适配,遗留问题:navigationbar 没有light模式
Browse files Browse the repository at this point in the history
  • Loading branch information
lelelongwang committed Jul 27, 2021
1 parent e02bddd commit b07f688
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ WanJetpack
- 本demo中的WebView适配了深色主题。

- 启动界面:(在Android10上调试)
- 方案:通过windowSplashscreenContent属性或者SplashActivity界面
- 注意 windowSplashscreenContent属性是在Android8.0(v26)上才有的,如果在之前的版本上适配启动界面,应该新增个Activity,即 SplashActivity
- 冷启动、热启动
- Splash Screen:展示品牌LogoSlogan
- 如果只是单纯的显示个界面,只需要在themes里设置<item name="android:windowSplashscreenContent">@color/jetpack_green_500</item>即可。
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {

defaultConfig {
applicationId "com.longjunhao.wanjetpack"
minSdkVersion 26
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/longjunhao/wanjetpack/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,20 @@ class MainActivity : AppCompatActivity() {
} catch (e: Exception) {
}*/

// todo 后期可以把沉侵式统一在themes中设置,或者统一在代码中设置
// todo:
// 1.后期可以把沉侵式统一在themes中设置,或者统一在代码中设置
// 2.Android6.0上没有SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR属性
when {
Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1 -> {
//以下代码实现了Android8.1上导航栏和状态栏的沉侵式,但是要结合themes中的设置
Build.VERSION.SDK_INT in Build.VERSION_CODES.M..Build.VERSION_CODES.O_MR1 -> {
//以下代码实现了Android6.0.1 、 Android8.1上导航栏和状态栏的沉侵式,但是要结合themes中的设置
window.clearFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
//window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS )
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class HomeViewPagerFragment : Fragment() {

private fun getTabIcon(position: Int): Int{
return when (position) {
HOME_ARTICLE_INDEX -> R.drawable.ic_launcher_foreground
VIEW_PAGER_INDEX_TWO -> R.drawable.ic_launcher_foreground
HOME_ARTICLE_INDEX -> R.drawable.ic_tab_selector
VIEW_PAGER_INDEX_TWO -> R.drawable.ic_tab_selector
else -> throw IndexOutOfBoundsException()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WechatFragment : Fragment() {
// Set the icon and text for each tab
TabLayoutMediator(binding.tabs, binding.viewPager) { tab, position ->
tab.text = titles[position]
tab.setIcon(R.drawable.ic_launcher_foreground)
tab.setIcon(R.drawable.ic_tab_selector)
}.attach()
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_fire_fill_active.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:fillColor="?attr/colorOnPrimary"
android:pathData="M834.1,469.2c-19.5,-43.8 -47.4,-82.6 -82.9,-115.2l-29.1,-26.7c-4.3,-3.8 -11.1,-2.1 -13,3.3l-13,37.3c-8.1,23.4 -23,47.3 -44.1,70.8 -1.4,1.5 -3,1.9 -4.1,2 -1.1,0.1 -2.8,-0.1 -4.3,-1.5 -1.4,-1.2 -2.1,-3 -2,-4.8 3.7,-60.2 -14.3,-128.1 -53.7,-202C555.3,171 510,123.1 453.4,89.7l-41.3,-24.3c-5.4,-3.2 -12.3,1 -12,7.3l2.2,48c1.5,32.8 -2.3,61.8 -11.3,85.9 -11,29.5 -26.8,56.9 -47,81.5 -14.2,17.2 -30.1,32.7 -47.5,46.1 -42.1,32.3 -76.8,74.4 -100.3,121.5C172.5,503.3 160,556.6 160,610c0,47.2 9.3,92.9 27.7,136 17.8,41.5 43.2,78.9 75.5,110.9 32.4,32 70,57.2 111.9,74.7C418.5,949.8 464.5,959 512,959s93.5,-9.2 136.9,-27.3c41.9,-17.5 79.6,-42.6 111.9,-74.7 32.4,-32 57.8,-69.4 75.5,-110.9 18.4,-43.1 27.7,-88.8 27.7,-136 0,-48.8 -10,-96.2 -29.9,-140.9z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_fire_inactive.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:fillColor="?attr/colorPrimaryDark"
android:pathData="M834.1,469.2c-19.5,-43.8 -47.4,-82.6 -82.9,-115.2l-29.1,-26.7c-4.3,-3.8 -11.1,-2.1 -13,3.3l-13,37.3c-8.1,23.4 -23,47.3 -44.1,70.8 -1.4,1.5 -3,1.9 -4.1,2 -1.1,0.1 -2.8,-0.1 -4.3,-1.5 -1.4,-1.2 -2.1,-3 -2,-4.8 3.7,-60.2 -14.3,-128.1 -53.7,-202C555.3,171 510,123.1 453.4,89.7l-41.3,-24.3c-5.4,-3.2 -12.3,1 -12,7.3l2.2,48c1.5,32.8 -2.3,61.8 -11.3,85.9 -11,29.5 -26.8,56.9 -47,81.5 -14.2,17.2 -30.1,32.7 -47.5,46.1 -42.1,32.3 -76.8,74.4 -100.3,121.5C172.5,503.3 160,556.6 160,610c0,47.2 9.3,92.9 27.7,136 17.8,41.5 43.2,78.9 75.5,110.9 32.4,32 70,57.2 111.9,74.7C418.5,949.8 464.5,959 512,959s93.5,-9.2 136.9,-27.3c41.9,-17.5 79.6,-42.6 111.9,-74.7 32.4,-32 57.8,-69.4 75.5,-110.9 18.4,-43.1 27.7,-88.8 27.7,-136 0,-48.8 -10,-96.2 -29.9,-140.9zM713,808.5c-53.7,53.2 -125,82.4 -201,82.4s-147.3,-29.2 -201,-82.4c-53.5,-53.1 -83,-123.5 -83,-198.4 0,-43.5 9.8,-85.2 29.1,-124 18.8,-37.9 46.8,-71.8 80.8,-97.9 21.5,-16.4 41.2,-35.5 58.6,-56.8 25,-30.5 44.6,-64.5 58.2,-101 5.4,-14.5 9.5,-30 12.1,-46.5 24.1,22.2 44.3,49 61.2,80.4 33.4,62.6 48.8,118.3 45.8,165.7 -1.4,22.8 7.5,44.5 24.4,59.8 14.7,13.2 33.7,19.9 53.4,18.8 19.7,-1 37.8,-9.7 51,-24.4 13.3,-14.9 24.8,-30.1 34.4,-45.6 14,17.9 25.7,37.4 35,58.4 15.9,35.8 24,73.9 24,113.1 0,74.9 -29.5,145.4 -83,198.4z"/>
</vector>
21 changes: 21 additions & 0 deletions app/src/main/res/drawable/ic_tab_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_fire_fill_active" android:state_selected="true"/>
<item android:drawable="@drawable/ic_fire_inactive"/>
</selector>
24 changes: 24 additions & 0 deletions app/src/main/res/values-night-v26/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Base.Theme.WanJetpack" parent="Theme.MaterialComponents.DayNight.NoActionBar">

<item name="colorPrimary">@color/jetpack_green_500</item>
<item name="colorOnPrimary">@color/jetpack_yellow_300</item>
<item name="colorPrimaryDark">@color/jetpack_green_700</item>
<item name="colorSecondary">@color/jetpack_yellow_300</item>
<item name="colorSurface">@color/jetpack_green_900</item>
<item name="colorOnSurface">@color/jetpack_white</item>
<item name="colorAccent">@color/jetpack_green_300</item>

<!-- 沉侵式 -->
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowLightStatusBar">true</item>

<!-- 启动界面 -->
<item name="android:windowSplashscreenContent">@drawable/ic_splash_content</item>
</style>
</resources>
4 changes: 2 additions & 2 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<!--<item name="android:colorBackground">@color/jetpack_green_900</item>-->

<!-- 沉侵式 -->
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

<!-- 启动界面 -->
<item name="android:windowSplashscreenContent">@drawable/ic_splash_content</item>
<!--<item name="android:windowSplashscreenContent">@drawable/ic_splash_content</item>-->
</style>

<style name="Theme.WanJetpack" parent="Base.Theme.WanJetpack"/>
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/values-v26/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Base.Theme.WanJetpack" parent="Theme.MaterialComponents.DayNight.NoActionBar">

<item name="colorPrimary">@color/jetpack_green_500</item>
<item name="colorOnPrimary">@color/jetpack_yellow_500</item>
<item name="colorPrimaryDark">@color/jetpack_green_700</item>
<item name="colorSecondary">@color/jetpack_yellow_500</item>
<item name="colorSurface">@color/jetpack_gray_50</item>
<item name="colorOnSurface">@color/jetpack_black</item>
<item name="colorAccent">@color/jetpack_green_700</item>

<!-- 沉侵式 -->
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowLightStatusBar">true</item>

<!-- 启动界面 -->
<item name="android:windowSplashscreenContent">@drawable/ic_splash_content</item>
</style>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ Floats:
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowLightStatusBar">true</item>

<!-- <item name="android:windowDrawsSystemBarBackgrounds">true</item>-->

<!-- 启动界面 -->
<item name="android:windowSplashscreenContent">@drawable/ic_splash_content</item>
<!--<item name="android:windowSplashscreenContent">@drawable/ic_splash_content</item>-->
</style>

<style name="Theme.WanJetpack" parent="Base.Theme.WanJetpack"/>
Expand Down

0 comments on commit b07f688

Please sign in to comment.