如何将Android SnackBar调整为屏幕上的特定位置 [英] How can you adjust Android SnackBar to a specific position on screen
问题描述
跟随新的android快餐栏,我试图将其设置为位于特定的y坐标上.似乎甚至不可能.
Follwing the new android snackbar, i'm trying to set it to be positioned on a specific y coordinate. Its seems to be not even a possible.
我已经尝试过获取快餐栏视图的父级,但是,对于父级来设置它的位置,没有任何事情要做.
I've tried with getting the parent of the snackbar's view, but, there's nothing to be done to the parent for it to set the position of it.
mSnackBar.getView().getParent();
在研究实际的类时,有一个mView实例和一个mParent实例,它们是私有的,无论如何都无法访问.
When digging into the actual class, there's an instance of mView, and mParent which is private, and can't be reached anyhow.
https://developer.android.com/reference/android/support/design/widget/Snackbar.html
推荐答案
可以通过在现有活动"布局中放置android.support.design.widget.CoordinatorLayout
来设置小吃栏的显示位置.
It is possible to set the location that the Snackbar is displayed by positioning a android.support.design.widget.CoordinatorLayout
within your existing Activity layout.
例如,假设您的现有布局是RelativeLayout
,则可以如下添加CoordinatorLayout
:
For example, say your existing layout is a RelativeLayout
you could add a CoordinatorLayout
as follows:
<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()
命令的第一个参数传递.
Then, make sure you pass the CoordinatorLayout
as the first argument of the Snackbar.make()
command.
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
的底部.
This will result in the Snackbar being shown at the bottom of the CoordinatorLayout
provided to the make() function.
如果传递的不是CoordinatorLayout
的View
,则Snackbar会沿着树走直到找到CoordinatorLayout
或布局的根.
If you pass a View
that is not a CoordinatorLayout
the Snackbar will walk up the tree until it finds a CoordinatorLayout
or the root of the layout.
这篇关于如何将Android SnackBar调整为屏幕上的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!