将 Snackbar 移到 bottomNavigationView 上方
Move Snackbar above the bottomNavigationView
想将我的小吃栏移到 bottomnavigationview 上方
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activityRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarView"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_size"
android:background="@color/color_primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</FrameLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNavigationLayout"
android:layout_below="@+id/appBarLayout" />
<LinearLayout
android:id="@+id/bottomNavigationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<View style="@style/FullDivider" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@android:color/white"
app:itemIconTint="@drawable/selector_bottom_bar_item"
app:itemTextColor="@color/blue"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/main_navigation" />
</LinearLayout>
<com.mandarine.features.security.UnlockAppInputView
android:id="@+id/unlockAppInputView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
在MainActivity
中这样写:
container?.let {
Snackbar.make(it, "No internet connection!", Snackbar.LENGTH_LONG).show()
}
但是 snackbar 也会从屏幕的 bot 显示
在 BottomNavigationView 上方添加一个 Layout
示例:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myLayout"
android:layout_above="@id/bottomNavigationView">
</android.support.design.widget.CoordinatorLayout>
然后在 Java 代码中使用:
final View viewPos = findViewById(R.id.myLayout);
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action_undo, showListener)
.show();
我认为问题出在 findSuitableParent 函数中。正如您在 232 行看到的那样,您的容器用作后备,但随后找到了根视图。尝试将您的容器更改为 CoordinatorLayout,它会解决问题。
使用下一个解决方案解决我的问题:
private fun showNetworkMessage(isConnected: Boolean) {
if (!isConnected) {
val snack = Snackbar.make(
findViewById(R.id.container),
this.getText(R.string.warning_no_internet_connection), Snackbar.LENGTH_LONG
)
val params = snack.view.layoutParams as FrameLayout.LayoutParams
params.setMargins(0, 0, 0, this.resources.getDimension(R.dimen.action_bar_size).toInt())
snack.view.layoutParams = params
snack.show()
}
}
在哪里你可以看到我没有使用 CoordinatorLayout
。
想将我的小吃栏移到 bottomnavigationview 上方
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activityRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarView"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_size"
android:background="@color/color_primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</FrameLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNavigationLayout"
android:layout_below="@+id/appBarLayout" />
<LinearLayout
android:id="@+id/bottomNavigationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<View style="@style/FullDivider" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@android:color/white"
app:itemIconTint="@drawable/selector_bottom_bar_item"
app:itemTextColor="@color/blue"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/main_navigation" />
</LinearLayout>
<com.mandarine.features.security.UnlockAppInputView
android:id="@+id/unlockAppInputView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
在MainActivity
中这样写:
container?.let {
Snackbar.make(it, "No internet connection!", Snackbar.LENGTH_LONG).show()
}
但是 snackbar 也会从屏幕的 bot 显示
在 BottomNavigationView 上方添加一个 Layout
示例:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myLayout"
android:layout_above="@id/bottomNavigationView">
</android.support.design.widget.CoordinatorLayout>
然后在 Java 代码中使用:
final View viewPos = findViewById(R.id.myLayout);
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action_undo, showListener)
.show();
我认为问题出在 findSuitableParent 函数中。正如您在 232 行看到的那样,您的容器用作后备,但随后找到了根视图。尝试将您的容器更改为 CoordinatorLayout,它会解决问题。
使用下一个解决方案解决我的问题:
private fun showNetworkMessage(isConnected: Boolean) {
if (!isConnected) {
val snack = Snackbar.make(
findViewById(R.id.container),
this.getText(R.string.warning_no_internet_connection), Snackbar.LENGTH_LONG
)
val params = snack.view.layoutParams as FrameLayout.LayoutParams
params.setMargins(0, 0, 0, this.resources.getDimension(R.dimen.action_bar_size).toInt())
snack.view.layoutParams = params
snack.show()
}
}
在哪里你可以看到我没有使用 CoordinatorLayout
。