Nem*_*bia 1 android view set button coordinates
我正在使用API 8进行编码.
我需要从View中获取坐标X和Y,并将它们设置为新Button的坐标.
我试过不同的方法但没有任何作用......
setX和getX方法仅适用于api级别11,我需要一种方法在API8上执行此操作.
这是我的方法,允许创建一个按钮的可拖动副本(此副本是一个imageview,当我删除它成为一个新按钮)
public boolean dragCopy(View view, MotionEvent me, Button bt){
int x,y,w,h,id,t,l;
int cont=-1;
int coord[] = new int[2];
bt2=new Button(this);
id = bt.getId();
bt2.setId(id);
Resources res = getResources();
cont = FindCont(bt, vector);
dr = getLetter(bt, dr, res, vector, cont);
w=dr.getIntrinsicWidth();
h=dr.getIntrinsicHeight();
if (me.getAction() == MotionEvent.ACTION_DOWN) {
//clicked on the button. DRAG START
status = START_DRAGGING;
image = new ImageView(this);
//set image drawable
image.setImageDrawable(dr);
image.setPadding((int)bt.getLeft(),(int)bt.getTop(), 0, 0);
layout.addView(image, params);
}
if (me.getAction() == MotionEvent.ACTION_UP) {
//button released. DROP
status = STOP_DRAGGING;
x = (int) me.getRawX();
y = (int) me.getRawY();
//create button compy from image
bt2.setBackgroundDrawable(dr);
bt2.setVisibility(View.VISIBLE);
//PROBLEMS
t= image.getTop();
l= image.getLeft();
bt2.setPadding(l, t, 0, 0);
System.out.println("**************** T: "+t +"--- L: "+l);
image.setVisibility(View.GONE);
bt2.setLayoutParams(image.getLayoutParams());
bt2.setWidth(w);
bt2.setHeight(h);
layout.addView(bt2);
bt2.setDrawingCacheEnabled(true);
bt2.setOnTouchListener(this);
bt2.invalidate();
image.invalidate();
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
//i'm moving the image
if (status == START_DRAGGING) {
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我没试过这个,但我认为它会起作用:
int buttonX = 50; // Arbitrary values - use whatever you want
int buttonY = 100;
int viewX = myView.getLeft();
int viewY = myView.getTop();
Button newButton = new Button();
newButton.setPadding(buttonX - viewX, buttonY - viewY, 0, 0);
// Other button setup
Run Code Online (Sandbox Code Playgroud)
这将在屏幕上将其设置为(50,100)的绝对位置.如果你希望它相对于你的布局的角是(50,100)那么使用这个:
int buttonX = 50; // Arbitrary values - use whatever you want
int buttonY = 100;
Button newButton = new Button();
newButton.setPadding(buttonX, buttonY, 0, 0);
// Other button setup
Run Code Online (Sandbox Code Playgroud)
这仅适用于不自动定位其子内容的布局,例如RelativeLayout.
| 归档时间: |
|
| 查看次数: |
9240 次 |
| 最近记录: |