gpt4 book ai didi

android - 如何将 Android SnackBar 调整到屏幕上的特定位置

转载 作者:IT王子 更新时间:2023-10-28 23:34:06 26 4
gpt4 key购买 nike

按照新的 android snackbar ,我正在尝试将其设置为定位在特定的 y 坐标上。这似乎是不可能的。

我已经尝试获取 snackbar View 的父级,但是,对于父级没有任何操作可以设置它的位置。

mSnackBar.getView().getParent();

在深入实际类时,有一个 mView 的实例,而 mParent 是私有(private)的,无论如何都无法访问。

https://developer.android.com/reference/android/support/design/widget/Snackbar.html

最佳答案

可以通过在现有的 Activity 布局中定位 android.support.design.widget.CoordinatorLayout 来设置 Snackbar 的显示位置。

例如,假设您现有的布局是 RelativeLayout,您可以添加一个 CoordinatorLayout,如下所示:

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/myCoordinatorLayout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.design.widget.CoordinatorLayout>

然后,确保将 CoordinatorLayout 作为 Snackbar.make() 命令的第一个参数传递。

final View viewPos = findViewById(R.id.myCoordinatorLayout);    
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action_undo, showListener)
.show();

这将导致 Snackbar 显示在提供给 make() 函数的 CoordinatorLayout底部

如果你传递一个不是 CoordinatorLayoutView,Snackbar 会沿着树向上走,直到找到 CoordinatorLayout 或布局。

关于android - 如何将 Android SnackBar 调整到屏幕上的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31492351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com