Android 获取错误的x和y坐标
这里是我用来获取ImageButton click listener的xy值以显示快速动作视图的方法,这是显示动作视图的方法的代码Android 获取错误的x和y坐标,android,android-layout,Android,Android Layout,这里是我用来获取ImageButton click listener的xy值以显示快速动作视图的方法,这是显示动作视图的方法的代码 protected void setQuickActionListener(View button) { float density = mActivity.getResources().getDisplayMetrics().density; WindowManager.LayoutParams wmlp = mActivity.getWindo
protected void setQuickActionListener(View button) {
float density = mActivity.getResources().getDisplayMetrics().density;
WindowManager.LayoutParams wmlp = mActivity.getWindow().getAttributes();
button.getLocationInWindow(location);
Toast.makeText(mActivity, " l1 " + location[0] + " l2 " + location[1],Toast.LENGTH_LONG).show();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = location[0]+ (int) (((button.getWidth() / 2) + button.getHeight() / 4) / density);
wmlp.y = location[1] + (int) ((button.getHeight() - 60) / density);
mQuickAction.show(button, (int) wmlp.x, (int) wmlp.y);
}
问题是我在错误的位置得到了快速行动视图,即在0,0处,偶尔在infalted布局上得到一次。你知道为什么会发生这种情况吗*
***
***
每个视图对象/子类都可以访问执行此操作的方法
<LinearLayout
android:id="@+id/lyt_xx"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="horizontal"
android:padding="@dimen/padding_small" >
<ImageView
android:id="@+id/img_separator"
android:layout_width="30dp"
android:layout_height="fill_parent"
android:layout_marginLeft="@dimen/padding_small"
android:layout_marginRight="@dimen/padding_small"
android:background="#333333" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
***<com.apps.horizontalgrid.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/xx_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:padding="@dimen/padding_small"
app:cacheColorHint="#00000000"
app:gravity="fill"
app:horizontalSpacing="@dimen/padding_small"
app:numColumns="auto_fit"
app:numRows="2"
app:rowHeight="260dp"
app:scrollDirectionLandscape="horizontal"
app:scrollDirectionPortrait="horizontal"
app:verticalSpacing="@dimen/padding_small" >
</com.apps.horizontalgrid.TwoWayGridView>***
</LinearLayout>
<!-- -->
</LinearLayout>
<!-- -->
在调用这些方法之前,您必须确保已显示视图
,否则将得到0
,或者最坏的情况是NPE
请记住,这些返回位置相对于视图的父视图。如果需要视图的实际绝对位置,则需要使用以下方法:
没有必要重新发明轮子。更多信息可在此处找到:您真正想要实现什么?请尝试View.getLocationOnScreen(),我在代码的第一部分和第二部分(anchor.getLocationOnScreen(location);)中使用了它。谢谢,但我之所以调用它是因为单击了已经显示的图像按钮。为什么我会得到NPE或0。
<LinearLayout
android:id="@+id/lyt_xx"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="horizontal"
android:padding="@dimen/padding_small" >
<ImageView
android:id="@+id/img_separator"
android:layout_width="30dp"
android:layout_height="fill_parent"
android:layout_marginLeft="@dimen/padding_small"
android:layout_marginRight="@dimen/padding_small"
android:background="#333333" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
***<com.apps.horizontalgrid.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/xx_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:padding="@dimen/padding_small"
app:cacheColorHint="#00000000"
app:gravity="fill"
app:horizontalSpacing="@dimen/padding_small"
app:numColumns="auto_fit"
app:numRows="2"
app:rowHeight="260dp"
app:scrollDirectionLandscape="horizontal"
app:scrollDirectionPortrait="horizontal"
app:verticalSpacing="@dimen/padding_small" >
</com.apps.horizontalgrid.TwoWayGridView>***
</LinearLayout>
<!-- -->
</LinearLayout>
<!-- -->
View.getTop(); // Top position of this view relative to its parent.
View.getLeft(); // Left position of this view relative to its parent.
View.getRight(); // Right position of this view relative to its parent.
View.getBottom(); // Bottom position of this view relative to its parent.
View.getLocationInWindow(); // Computes the coordinates of this view in its window.
or
View.getLocationOnScreen(); // Computes the coordinates of this view on the screen.