2014-02-21 3 views
0
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 클릭 수신기의 x y 값을 가져 와서 빠른 동작 뷰를 표시하는 방법과이 동작 뷰의 show 메서드 코드를 표시합니다.잘못된 x 및 y 좌표 얻기 android

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

문제는 내가 한 번 infalted 레이아웃 동안 0,0 즉 잘못된 postions에서 신속한 조치 뷰를 얻고있다이 아래에 발생하는가에 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

실제로 달성하고 싶은 것은 무엇입니까? – pskink

+0

try.getLocationOnScreen() – techieWings

+0

코드의 첫 번째와 두 번째 부분에서 사용했습니다. (anchor.getLocationOnScreen (location);) –

답변

0

각보기 개체/하위 클래스는 정확히이 작업을 수행하는 메서드에 액세스 할 수 있습니다.

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. 

당신은 그렇지 않으면 당신은 0 또는 최악의 NPE를 얻을 수 있습니다의 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

+1

감사하지만 이미 이미지 버튼을 클릭하고 있습니다. 그 이유는 무엇입니까? 나는 NPE 또는 0을 얻고있다. –