繁体   English   中英

错误的x和y坐标Android

[英]Getting wrong x and y coordinates Android

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);

}

这是我用来获取ImageButton单击侦听器的xy值的方法,以显示快速的动作视图,这是动作视图的show方法的代码。

    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);}

问题是我在错误的位置上获得了快速的操作视图,即在凹凸布局上一次出现在0,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对象/子类都可以访问执行此操作的方法。

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 ,否则将获得0或最坏的NPE。

请记住,这些返回位置相对于View的父级。 如果您想要View的实际绝对位置,则需要使用以下方法:

View.getLocationInWindow(); // Computes the coordinates of this view in its window.
or
View.getLocationOnScreen(); // Computes the coordinates of this view on the screen.

无需重新发明轮子。 可以在这里找到更多信息: http : //developer.android.com/reference/android/view/View.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM