gpt4 book ai didi

java - 检索 FAB 的 X 和 Y

转载 作者:行者123 更新时间:2023-12-02 11:32:14 26 4
gpt4 key购买 nike

我有一个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中我现在想要,屏幕上的fab位置在哪里。我根据这篇文章使用了这个方法: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 和 getPointOfView(fab).y 返回 0。我如何检索 X 和 Y fab 按钮?

最佳答案

您可以使用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);
}
}
})

关于java - 检索 FAB 的 X 和 Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209917/

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