获取错误的X和Y座标机器人 [英] Getting wrong x and y coodinates android
问题描述
保护无效setQuickActionListener(查看按钮){ 浮密度= mActivity.getResources()getDisplayMetrics()密度。
WindowManager.LayoutParams wmlp = mActivity.getWindow()的getAttributes()。
button.getLocationInWindow(位置);
Toast.makeText(mActivity,L1位置+ [0] +12+位置[1],Toast.LENGTH_LONG).show();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x =位置[0] +(int)的(((button.getWidth()/ 2)+ button.getHeight()/ 4)/密度);
wmlp.y =位置[1] +(int)的((button.getHeight() - 60)/密度);
mQuickAction.show(按钮,(INT)wmlp.x,(INT)wmlp.y);}
在这里我使用得到x和y的值的ImageButton的点击监听器来显示快速行动的意见的方法和这个code作秀方法采取行动的看法。
公共无效秀(查看主播,诠释XPOS,诠释yPos){
preShow(); mDidAction = FALSE; INT [] =位置INT新[2]; anchor.getLocationOnScreen(位置); 矩形anchorRect =新的矩形(位置[0],位置[1],位置[0] + anchor.getWidth(),位置[1]
+ anchor.getHeight()); mRootView.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); mRootView.measure(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); INT rootHeight = mRootView.getMeasuredHeight();
INT rootWidth = mRootView.getMeasuredWidth();
如果(rootWidth == 0){
rootWidth = mRootView.getMeasuredWidth();
} INT屏幕宽度= mWindowManager.getDefaultDisplay()的getWidth()。
INT screenHeight = mWindowManager.getDefaultDisplay()的getHeight()。 arrowPos = anchorRect.centerX() - XPOS; INT dytop = anchorRect.top;
INT dybottom = screenHeight / 4;
布尔onTop =(dytop> dybottom)?真假;
LinearLayout.LayoutParams rlpms;// XPOS> (屏幕宽度 - 100 INT dyleft = XPOS;
INT dyright =屏幕宽度 - 屏幕宽度/ 10; 布尔onRight =(dyleft> dyright)?真假; 如果(onRight){
如果(onTop){
yPos = anchorRect.top - rootHeight / 3;
IM1 =(ImageView的)mRootView.findViewById(R.id.arrow_down);
rlpms =新LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams LP =(android.widget.LinearLayout.LayoutParams)im1.getLayoutParams();
lp.setMargins(0,rootHeight / 3,0,0);
im1.setLayoutParams(LP);
Log.d(ddsaf,+ onTop);
}
其他
{
IM1 =(ImageView的)mRootView.findViewById(R.id.arrow_down);
rlpms =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams LP =(android.widget.LinearLayout.LayoutParams)im1.getLayoutParams();
lp.setMargins(0,5,0,0);
im1.setLayoutParams(LP);
//Log.d(\"ddsaf,+ onTop); }
XPOS = anchorRect.left - rootWidth;
}其他{
如果(onTop){
yPos = anchorRect.top - rootHeight / 3;
IM1 =(ImageView的)mRootView.findViewById(R.id.arrow_up);
rlpms =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams LP =(android.widget.LinearLayout.LayoutParams)im1.getLayoutParams();
lp.setMargins(0,rootHeight / 3,0,0);
im1.setLayoutParams(LP);
//Log.d(\"ddsaf,顶+ onTop);
}
其他
{
IM1 =(ImageView的)mRootView.findViewById(R.id.arrow_up);
rlpms =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams LP =(android.widget.LinearLayout.LayoutParams)im1.getLayoutParams();
lp.setMargins(0,5,0,0);
im1.setLayoutParams(LP);
//Log.d(\"ddsaf,+ onTop); }
XPOS = XPOS + rootWidth / 4;
}
Log.d(ddsaf,+ anchorRect.left);
showArrow(((onRight)R.id.arrow_down:R.id.arrow_up),arrowPos); setAnimationStyle(屏幕宽度,anchorRect.centerX(),onRight); mWindow.showAtLocation(锚,Gravity.NO_GRAVITY,XPOS,yPos);}
问题是我得到的错误的志愿服务岗位迅速行动意见,而在0,0,即,一旦上infalted布局任何想法,为什么发生这种情况下面是它已经infalted用的 * 的
<的LinearLayout
机器人:ID =@ + ID / lyt_xx
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =60
机器人:方向=横向
机器人:填充=@扪/ padding_small> < ImageView的
机器人:ID =@ + ID / img_separator
机器人:layout_width =30dp
机器人:layout_height =FILL_PARENT
机器人:layout_marginLeft =@扪/ padding_small
机器人:layout_marginRight =@扪/ padding_small
机器人:背景=#333333/> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向> ***< com.apps.horizontalgrid.TwoWayGridView
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID / xx_gridview
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@颜色/背景
机器人:填充=@扪/ padding_small
应用:cacheColorHint =#00000000
应用:比重=补
应用:horizontalSpacing =@扪/ padding_small
应用:为numColumns =auto_fit
应用:其行=2
应用:的rowHeight =260dp
应用:scrollDirectionLandscape =横向
应用:scrollDirectionPortrait =横向
应用:verticalSpacing =@扪/ padding_small>
< /com.apps.horizontalgrid.TwoWayGridView> ***
< / LinearLayout中>
&所述;! - - >
< / LinearLayout中> &所述;! - - >
每个视图对象/子类可以访问哪些做的正是这个方法。
View.getTop(); //这个视图相对于其父的顶部位置。
View.getLeft(); //这个视图相对于其父的左侧位置。
View.getRight(); //这个视图相对于其父的右侧位置。
View.getBottom(); //这个视图相对于其父的下方位置。
您必须确保查看
之前已经调用任何这些方法显示出来,否则,你会得到 0
或者最坏的NPE。
记住,相对于视图的父这些返回岗位。如果你想在实际绝对视图的位置,那么你将需要使用方法(S):
View.getLocationInWindow(); //计算这种观点在其窗口的坐标。
要么
View.getLocationOnScreen(); //计算在屏幕上这种观点的坐标。
有没有必要推倒重来。进一步的信息可以在这里找到: http://developer.android.com/reference/安卓/浏览/ View.html
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);
}
here the method i am using to get x y values for an ImageButton click listener to show quick action views and this the code for show method for action views.
public void show(View anchor, int xPos, int yPos) {
preShow();
mDidAction = false;
int[] location = new int[2];
anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1]
+ anchor.getHeight());
mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootHeight = mRootView.getMeasuredHeight();
int rootWidth = mRootView.getMeasuredWidth();
if (rootWidth == 0) {
rootWidth = mRootView.getMeasuredWidth();
}
int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
int screenHeight = mWindowManager.getDefaultDisplay().getHeight();
arrowPos = anchorRect.centerX()-xPos;
int dytop = anchorRect.top;
int dybottom = screenHeight/4;
boolean onTop = (dytop > dybottom) ? true : false;
LinearLayout.LayoutParams rlpms;
// xPos > (screenWidth - 100
int dyleft = xPos;
int dyright = screenWidth - screenWidth/10;
boolean onRight = (dyleft > dyright) ? true : false;
if (onRight) {
if(onTop){
yPos = anchorRect.top - rootHeight/3;
im1 =(ImageView) mRootView.findViewById(R.id.arrow_down);
rlpms=new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
lp.setMargins(0,rootHeight/3,0 ,0);
im1.setLayoutParams(lp);
Log.d("ddsaf", ""+onTop);
}
else
{
im1 =(ImageView) mRootView.findViewById(R.id.arrow_down);
rlpms=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
lp.setMargins(0,5,0 ,0);
im1.setLayoutParams(lp);
//Log.d("ddsaf", ""+onTop);
}
xPos = anchorRect.left - rootWidth;
} else {
if(onTop){
yPos = anchorRect.top - rootHeight/3;
im1 =(ImageView) mRootView.findViewById(R.id.arrow_up);
rlpms=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
lp.setMargins(0,rootHeight/3,0 ,0);
im1.setLayoutParams(lp);
//Log.d("ddsaf", "top "+onTop);
}
else
{
im1 =(ImageView) mRootView.findViewById(R.id.arrow_up);
rlpms=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
lp.setMargins(0,5,0 ,0);
im1.setLayoutParams(lp);
//Log.d("ddsaf", ""+onTop);
}
xPos = xPos+rootWidth/4;
}
Log.d("ddsaf", ""+anchorRect.left);
showArrow(((onRight) ? R.id.arrow_down : R.id.arrow_up), arrowPos);
setAnimationStyle(screenWidth, anchorRect.centerX(), onRight);
mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);}
problem is i am getting quick action views at wrong postions ie at 0,0 once in while on infalted layout any idea why this happens below is where it has been infalted to with *
<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>
<!-- -->
Each View object/subclass has access to methods which do exactly this.
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.
You have to ensure that the View
has been displayed prior to calling any of these methods, otherwise you'll get 0
or at worst an NPE.
Bear in mind, these return positions relative to a View's parent. If you want the actual absolute position of the View, then you will need to use the method(s):
View.getLocationInWindow(); // Computes the coordinates of this view in its window.
or
View.getLocationOnScreen(); // Computes the coordinates of this view on the screen.
There is no need to reinvent the wheel. Further information can be found here: http://developer.android.com/reference/android/view/View.html
这篇关于获取错误的X和Y座标机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!