2014-02-21 2 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); 

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

Проблема в том, я получаю быстрые взгляды действий на неправильных postions т.е. в 0,0 раз в то время как на infalted макете любая идея, почему это происходит ниже, где это было infalted к с *Неправильный x и y coodinates android

<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 View.getLocationOnScreen() – techieWings

+0

Я использовал его в первой и второй части кода (anchor.getLocationOnScreen (location);) –

ответ

0

Каждый объект/подкласс 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

+1

спасибо, но я вызываю его для щелчка кнопки изображения, которая уже отображается. Тогда я получаю NPE или 0. –

Смежные вопросы