背景
导航栏可以是透明的,这样内容将被绘制在它的后面(在 Z 坐标中),并且在它上面会有视图(在 Y 坐标中),就像在相机应用程序上一样:
对于相机应用程序,导航栏后面有内容(相机预览),上面有视图(按钮)
问题
我发现的每一个解决方案(还有很多,比如这里)并没有让我选择既拥有透明导航栏,又拥有其上方和背后的视图。
我试过的
这是我尝试使用(此处android:fitsSystemWindows="true"
的示例)解决它的一种方法:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
setContentView(R.layout.activity_main)
}
}
activity_main.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#33ff0000"
android:text="text behind nav bar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#3300ff00"
android:text="text above nav bar" android:fitsSystemWindows="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
样式.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
但结果是两个视图都在导航栏后面:
我发现可行的唯一方法是获取导航栏的高度,并将视图的下边距设置为具有此值,但这似乎是一个奇怪的解决方案,我想尝试避免 :
/**
* returns the natural orientation of the device: Configuration.ORIENTATION_LANDSCAPE or Configuration.ORIENTATION_PORTRAIT .<br></br>
* The result should be consistent no matter the orientation of the device
*/
fun getScreenNaturalOrientation(context: Context): Int {
with(context) {
//based on : http://stackoverflow.com/a/9888357/878126
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val config = resources.configuration
val rotation = windowManager.defaultDisplay.rotation
return if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT)
Configuration.ORIENTATION_LANDSCAPE
else
Configuration.ORIENTATION_PORTRAIT
}
}
fun getNavBarHeight(context: Context, defaultHeightToReturn: Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48f, context.resources.displayMetrics).toInt()): Int {
val resources = context.resources
val orientation = resources.configuration.orientation
val isTablet = getScreenNaturalOrientation(context) == Configuration.ORIENTATION_LANDSCAPE
val resourceId = if (isTablet)
resources.getIdentifier(if (orientation == Configuration.ORIENTATION_PORTRAIT) "navigation_bar_height" else "navigation_bar_height_landscape", "dimen", "android")
else
resources.getIdentifier(if (orientation == Configuration.ORIENTATION_PORTRAIT) "navigation_bar_height" else "navigation_bar_width", "dimen", "android")
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId)
}
return defaultHeightToReturn
}
问题
怎么会忽略
android:fitsSystemWindows
?如何在导航栏上方创建一个视图,在其后面创建一个视图,例如在相机应用程序上?我也不希望在旧的 Android 版本上无缘无故地有额外的空间。
编辑:关于为什么android:fitsSystemWindows
不起作用,我发现了这个和这个。但是,当我尝试通过仅使用 来使用此技术时CoordinatorLayout
,它仍然不适用于上述情况:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#33ff0000"
android:text="text behind nav bar" android:textSize="20sp" android:layout_gravity="bottom"
/>
<TextView android:layout_width="wrap_content" android:textSize="20sp" android:layout_gravity="bottom|end"
android:layout_height="wrap_content" android:background="#3300ff00"
android:text="text above nav bar" android:fitsSystemWindows="true"
/>
</android.support.design.widget.CoordinatorLayout>