优化导航栏位置

pull/1395/head
gedoor 3 years ago
parent d64f5b665c
commit 56c29dd993
  1. 11
      app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt
  3. 71
      app/src/main/java/io/legado/app/utils/ActivityExtensions.kt

@ -156,18 +156,21 @@ abstract class ReadBookBaseActivity :
} else { } else {
0 0
} }
when (navigationBarPos) { when (navigationBarGravity) {
POS.BOTTOM -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply { Gravity.BOTTOM -> layoutParams =
(layoutParams as FrameLayout.LayoutParams).apply {
height = navigationBarHeight height = navigationBarHeight
width = MATCH_PARENT width = MATCH_PARENT
gravity = Gravity.BOTTOM gravity = Gravity.BOTTOM
} }
POS.LEFT -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply { Gravity.LEFT -> layoutParams =
(layoutParams as FrameLayout.LayoutParams).apply {
height = MATCH_PARENT height = MATCH_PARENT
width = navigationBarHeight width = navigationBarHeight
gravity = Gravity.LEFT gravity = Gravity.LEFT
} }
POS.RIGHT -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply { Gravity.RIGHT -> layoutParams =
(layoutParams as FrameLayout.LayoutParams).apply {
height = MATCH_PARENT height = MATCH_PARENT
width = navigationBarHeight width = navigationBarHeight
gravity = Gravity.RIGHT gravity = Gravity.RIGHT

@ -1,9 +1,11 @@
package io.legado.app.ui.book.read package io.legado.app.ui.book.read
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View.OnClickListener import android.view.View.OnClickListener
import android.view.View.OnLongClickListener import android.view.View.OnLongClickListener
@ -305,6 +307,7 @@ class ReadMenu @JvmOverloads constructor(
binding.llBrightness.visible(showBrightnessView) binding.llBrightness.visible(showBrightnessView)
} }
@SuppressLint("RtlHardcoded")
override fun onAnimationEnd(animation: Animation) { override fun onAnimationEnd(animation: Animation) {
val navigationBarHeight = val navigationBarHeight =
if (ReadBookConfig.hideNavigationBar) { if (ReadBookConfig.hideNavigationBar) {
@ -315,12 +318,13 @@ class ReadMenu @JvmOverloads constructor(
binding.run { binding.run {
vwMenuBg.setOnClickListener { runMenuOut() } vwMenuBg.setOnClickListener { runMenuOut() }
root.padding = 0 root.padding = 0
when (activity?.navigationBarPos) { when (activity?.navigationBarGravity) {
POS.BOTTOM -> root.bottomPadding = navigationBarHeight Gravity.BOTTOM -> root.bottomPadding = navigationBarHeight
POS.LEFT -> root.leftPadding = navigationBarHeight Gravity.LEFT -> root.leftPadding = navigationBarHeight
POS.RIGHT -> root.rightPadding = navigationBarHeight Gravity.RIGHT -> root.rightPadding = navigationBarHeight
} }
} }
callBack.upSystemUiVisibility()
if (!LocalConfig.readMenuHelpVersionIsLast) { if (!LocalConfig.readMenuHelpVersionIsLast) {
callBack.showReadMenuHelp() callBack.showReadMenuHelp()
} }

@ -5,6 +5,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.view.* import android.view.*
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
@ -38,29 +39,34 @@ val Activity.windowSize: DisplayMetrics
return displayMetrics return displayMetrics
} }
/////以下方法需要在View完全被绘制出来之后调用,否则判断不了,在比如 onWindowFocusChanged()方法中可以得到正确的结果/////
/** /**
* 返回NavigationBar是否存在 * 返回NavigationBar
* 该方法需要在View完全被绘制出来之后调用否则判断不了
* 在比如 onWindowFocusChanged方法中可以得到正确的结果
*/ */
val Activity.isNavigationBarExist: Boolean val Activity.navigationBar: View?
get() { get() {
val viewGroup = (window.decorView as? ViewGroup) ?: return false val viewGroup = (window.decorView as? ViewGroup) ?: return null
for (i in 0 until viewGroup.childCount) { for (i in 0 until viewGroup.childCount) {
val childId = viewGroup.getChildAt(i).id val child = viewGroup.getChildAt(i)
val childId = child.id
if (childId != View.NO_ID if (childId != View.NO_ID
&& resources.getResourceEntryName(childId) == "navigationBarBackground" && resources.getResourceEntryName(childId) == "navigationBarBackground"
) { ) {
return true return child
} }
} }
return false return null
} }
/** /**
* 该方法需要在View完全被绘制出来之后调用否则判断不了 * 返回NavigationBar是否存在
* 在比如 onWindowFocusChanged方法中可以得到正确的结果 */
val Activity.isNavigationBarExist: Boolean
get() = navigationBar != null
/**
* 返回NavigationBar高度
*/ */
val Activity.navigationBarHeight: Int val Activity.navigationBarHeight: Int
get() { get() {
@ -72,47 +78,10 @@ val Activity.navigationBarHeight: Int
} }
/** /**
* 返回导航栏位置 * 返回navigationBar位置
*/ */
@Suppress("DEPRECATION") val Activity.navigationBarGravity: Int
val Activity.navigationBarPos: POS
get() { get() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val gravity = (navigationBar?.layoutParams as? FrameLayout.LayoutParams)?.gravity
val display: Display = windowManager.defaultDisplay return gravity ?: Gravity.BOTTOM
val rotate: Int = display.rotation
val height: Int = display.height
val width: Int = display.width
return if (width > height) {
if (rotate == Surface.ROTATION_270) {
POS.LEFT
} else {
POS.RIGHT
} }
} else {
POS.BOTTOM
}
} else {
val display: Display = windowManager.defaultDisplay
val metricsReal = DisplayMetrics()
display.getRealMetrics(metricsReal)
val metricsCurrent = DisplayMetrics()
display.getMetrics(metricsCurrent)
val currH = metricsCurrent.heightPixels
val currW = metricsCurrent.widthPixels
val realW = metricsReal.widthPixels
val realH = metricsReal.heightPixels
return if (currW != realW && currH == realH) {
POS.RIGHT
} else {
POS.BOTTOM
}
}
}
enum class POS {
BOTTOM, LEFT, RIGHT
}
Loading…
Cancel
Save