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方法的代码。
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>
<!-- -->
答案 0 :(得分: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