在AbsoluteLayout上的DragEvent.ACTION_DROP上准确地将按钮膨胀到触摸点的中心

时间:2012-12-12 08:47:31

标签: android drag-and-drop android-view layout-inflater absolutelayout

我希望按钮的中心点=触摸点充气。 在膨胀时,我做如下:

control = inflater.inflate(R.layout.button,
                    (ViewGroup) target_layout, false);
targetContainer.addView(control);

    x = x - (control.getWidth()/2);   // control not inflating exactly @ center of 
    y = y - (control.getHeight()/2); //  the touch left.
    absoluteLayoutParams = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, x, y);
    control.setLayoutParams(absoluteLayoutParams);

button.xml 包含高度和宽度=包裹内容的按钮。 x & y 这是释放阻力时的触摸坐标。


This is how its getting inflated这就是它从触摸点膨胀的方式。

I expect it to get inflated like this我希望它从触摸点这样膨胀。

先谢谢,卡皮尔。

1 个答案:

答案 0 :(得分:0)

尝试致电

targetContainer.addView(control); 

为它设置layoutParams后,如下所示:

control = inflater.inflate(R.layout.button,
                (ViewGroup) target_layout, false);

x = x - (control.getWidth()/2);   // control not inflating exactly @ center of 
y = y - (control.getHeight()/2); //  the touch left.
absoluteLayoutParams = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, x, y);
control.setLayoutParams(absoluteLayoutParams);

targetContainer.addView(control);

也许这就是问题。