【发布时间】:2010-09-24 22:36:57
【问题描述】:
我想更改应用程序中按钮的边距... 假设我在线性布局中有 5 个这样的按钮,一个在 d 下方...
当我关注一个按钮时,它的宽度应该增加(我知道如何处理),同时宽度应该向左和向右线性增加。
即如果按钮的当前宽度为 250,x 坐标为 50(这意味着按钮的左边是 50,按钮的右边是 300),当我关注按钮时,我应该得到 x 坐标为 75(左 = 25 和右 = 325)。如何改变按钮的margin/x坐标?
【问题讨论】:
我想更改应用程序中按钮的边距... 假设我在线性布局中有 5 个这样的按钮,一个在 d 下方...
当我关注一个按钮时,它的宽度应该增加(我知道如何处理),同时宽度应该向左和向右线性增加。
即如果按钮的当前宽度为 250,x 坐标为 50(这意味着按钮的左边是 50,按钮的右边是 300),当我关注按钮时,我应该得到 x 坐标为 75(左 = 25 和右 = 325)。如何改变按钮的margin/x坐标?
【问题讨论】:
查看http://developer.android.com/reference/android/view/animation/Animation.html 和http://developer.android.com/reference/android/view/animation/ScaleAnimation.html 的Animation 和ScaleAnimation 课程。
一种方法是在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:fromXScale 和android:toXScale 的值,或者随意设置它们。要将动画应用到您的按钮,
Button myButton = (Button)findViewById(R.id.myButton);
myButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.makeLarger));
当然,makeLarger 和 makeSmaller 动画应该在您触摸按钮时设置。
我希望这会有所帮助!
【讨论】: