我在平板电脑的ICS上有代码。我想动态地将Imageview添加到片段中。该片段已包含首选项列表。动态添加视图的代码如下。我正在编写这种拖放功能。这部分代码取自Android音乐应用程序的TouchInterceptor.java文件。
mWindowParams = new WindowManager.LayoutParams();
mWindowParams.gravity = Gravity.TOP ;
mWindowParams.x = 0;
mWindowParams.y = y
mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mWindowParams.format = PixelFormat.TRANSLUCENT;
mWindowParams.windowAnimations = 0;
ImageView v = new ImageView(mContext);
int backGroundColor = mContext.getResources().getColor(R.color.bg_background);
v.setBackgroundColor(backGroundColor);
v.setImageBitmap(bm);
mWindowManager = (WindowManager)mContext.getSystemService("window");
mWindowManager.addView(v, mWindowParams);
我将windowlayout参数的x坐标指定为0.当显示视图时,它不是从片段的左侧显示(右窗格),而是从右侧窗格的中间显示视图并且正在跨越到右侧窗格。我做错了什么?必须采取哪些措施来纠正这个问题?
答案 0 :(得分:8)
尝试将重力设置为Gravity.TOP | Gravity.LEFT
答案 1 :(得分:2)
我正在使用旧版Google Music开源项目中的相同代码进行了大量工作。
就像我猜的那样,我在片段中有一个TouchInterceptor
。我的问题是片段没有占用所有的屏幕,它的左边有另一个片段。
我使用mWindowParams.gravity
尝试了很多能够指定绝对x,y但我找不到解决方案。最终我用mWindowParams.horizontalMargin
解决了它。
以下是我提出的代码:
private int getAbsoluteLeft(){
ViewGroup parent;
View child = this;
int left_absolute_position = 0;
try{
while( (parent = (ViewGroup) child.getParent()) != null) {
left_absolute_position += child.getLeft();
child = parent;
}
}catch(ClassCastException e){
}
return left_absolute_position;
}
private void startDragging(Bitmap bm, int x, int y) {
stopDragging();
//build the view
Context context = getContext();
ImageView v = new ImageView(context);
//int backGroundColor = context.getResources().getColor(R.color.dragndrop_background);
//v.setBackgroundColor(backGroundColor);
//v.setBackgroundResource(R.drawable.playlist_tile_drag);
v.setPadding(0, 0, 0, 0);
v.setImageBitmap(bm);
mDragBitmap = bm;
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
//Build Params
mWindowParams = new WindowManager.LayoutParams();
mWindowParams.gravity = Gravity.TOP | Gravity.LEFT;
mWindowParams.x = ( x - mDragPointX + mXOffset );
mWindowParams.y = ( y - mDragPointY + mYOffset );
DisplayMetrics metrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(metrics);
mWindowParams.horizontalMargin = (float)getAbsoluteLeft() / (float)metrics.widthPixels;
mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
mWindowParams.format = PixelFormat.TRANSLUCENT;
mWindowParams.windowAnimations = 0;
mWindowManager.addView(v, mWindowParams);
mDragView = v;
}
我添加的部分是:
DisplayMetrics metrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(metrics);
mWindowParams.horizontalMargin = (float)getAbsoluteLeft() / (float)metrics.widthPixels;
此参数mWindowParams.horizontalMargin
将指定重力侧(Gravity.Left
)的窗口边距指定为%€[0,1]。
因此,使用getAbsoluteLeft()
,我会转到View树,从左侧获取列表的绝对距离。
将其除以窗口的宽度,即可获得所需的保证金百分比。