优化导航栏位置

pull/1395/head
gedoor 3 years ago
parent d64f5b665c
commit 56c29dd993
  1. 35
      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,22 +156,25 @@ abstract class ReadBookBaseActivity :
} else {
0
}
when (navigationBarPos) {
POS.BOTTOM -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply {
height = navigationBarHeight
width = MATCH_PARENT
gravity = Gravity.BOTTOM
}
POS.LEFT -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply {
height = MATCH_PARENT
width = navigationBarHeight
gravity = Gravity.LEFT
}
POS.RIGHT -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply {
height = MATCH_PARENT
width = navigationBarHeight
gravity = Gravity.RIGHT
}
when (navigationBarGravity) {
Gravity.BOTTOM -> layoutParams =
(layoutParams as FrameLayout.LayoutParams).apply {
height = navigationBarHeight
width = MATCH_PARENT
gravity = Gravity.BOTTOM
}
Gravity.LEFT -> layoutParams =
(layoutParams as FrameLayout.LayoutParams).apply {
height = MATCH_PARENT
width = navigationBarHeight
gravity = Gravity.LEFT
}
Gravity.RIGHT -> layoutParams =
(layoutParams as FrameLayout.LayoutParams).apply {
height = MATCH_PARENT
width = navigationBarHeight
gravity = Gravity.RIGHT
}
}
visible()
} else {

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

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