动态改变按钮的X和Y位置/改变按钮的边距

时间:2010-09-24 11:18:20

标签: android margin

我想在d applcn中更改按钮的边距... 假设我在线性布局中有5个这样的按钮,其中一个低于其他...

当我专注于一个按钮时,它的宽度应该增加(我知道如何处理),同时宽度应该向左和向右线性增加。

即。如果按钮的当前宽度为250,x坐标为50(这意味着按钮的左边是50,按钮的右边是300),当我专注于按钮时,我应该得到x坐标为75(左= 25和对= 325)。如何更改按钮的margin / x坐标?

1 个答案:

答案 0 :(得分:1)

查看http://developer.android.com/reference/android/view/animation/Animation.htmlhttp://developer.android.com/reference/android/view/animation/ScaleAnimation.html上的AnimationScaleAnimation课程。

一种方法是在res/anim目录中定义两个XML文件,一个用于使按钮变大,另一个用于缩小按钮。所以makeLarger.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
android:fromXScale="1" 
android:toXScale="1.1" 
android:pivotX="50%" 
android:pivotY="50%" 
android:duration="200" 
android:fillAfter="true" />

在这里,你将宽度增加了10%,从1.0增加到1.1。对于makeSmaller.xml文件,请交换android:fromXScaleandroid:toXScale的值,或者根据需要设置它们。要将动画应用于按钮,

Button myButton = (Button)findViewById(R.id.myButton);
myButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.makeLarger));

当然,触摸按钮时应设置makeLarger和makeSmaller动画。

我希望这有帮助!