检索 FAB 的 X 和 Y
Retrieve the X & Y of FAB
我有一个 RelativeLayout 并且在这个布局中我有很棒的按钮:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="8dp"/>
现在,在 java 我想现在 screen.I 上的 fab 位置在哪里根据这个 post 使用此方法:enter link description here
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
并像这样使用此方法:
fab = findViewById(R.id.fab);
for (int i = 0; i < pv.length; i++) {
pv[i] = new Point(getPointOfView(fab).x ,getPointOfView(fab).y);
}
但是 getPointOfView(fab).x and getPointOfView(fab).y
return 0.how 我可以找回 X 和 Y fab 按钮吗?
使用
View.getLocationOnScreen()
获取值的方法。
注意:尝试在处理程序中使用此方法
new Handler ().post(...... v.getLocationOnScreen(location); .....);
onCreate 方法是早期获取位置我们应该在 activity 的 onWindowFocusChanged()
方法中尝试此方法,当应用程序结束所有与视图相关的任务时。
reference
我发现,我怎样才能得到好的职位:
fab.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
for (int i = 0; i < pv.length; i++) {
pv[i] = new Point(getPointOfView(fab).x, getPointOfView(fab).y);
}
fab.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
但如果我在删除 GloabalLayout 地方时出错,请告诉我。
您可以使用 View#post()
API,然后您的代码将被替换为以下代码:
FloatingActionButton fab = findViewById(R.id.fab);
fab.post(new Runnable() {
@Override
public void run() {
// this callback will be executed after view is laid out
for (int i = 0; i < pv.length; ++i) {
pv[i] = new Point(getPointOfView(fab).x ,getPointOfView(fab).y);
}
}
})
我有一个 RelativeLayout 并且在这个布局中我有很棒的按钮:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="8dp"/>
现在,在 java 我想现在 screen.I 上的 fab 位置在哪里根据这个 post 使用此方法:enter link description here
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
并像这样使用此方法:
fab = findViewById(R.id.fab);
for (int i = 0; i < pv.length; i++) {
pv[i] = new Point(getPointOfView(fab).x ,getPointOfView(fab).y);
}
但是 getPointOfView(fab).x and getPointOfView(fab).y
return 0.how 我可以找回 X 和 Y fab 按钮吗?
使用
View.getLocationOnScreen()
获取值的方法。 注意:尝试在处理程序中使用此方法
new Handler ().post(...... v.getLocationOnScreen(location); .....);
onCreate 方法是早期获取位置我们应该在 activity 的 onWindowFocusChanged()
方法中尝试此方法,当应用程序结束所有与视图相关的任务时。
reference
我发现,我怎样才能得到好的职位:
fab.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
for (int i = 0; i < pv.length; i++) {
pv[i] = new Point(getPointOfView(fab).x, getPointOfView(fab).y);
}
fab.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
但如果我在删除 GloabalLayout 地方时出错,请告诉我。
您可以使用 View#post()
API,然后您的代码将被替换为以下代码:
FloatingActionButton fab = findViewById(R.id.fab);
fab.post(new Runnable() {
@Override
public void run() {
// this callback will be executed after view is laid out
for (int i = 0; i < pv.length; ++i) {
pv[i] = new Point(getPointOfView(fab).x ,getPointOfView(fab).y);
}
}
})