【问题标题】:Changing X and Y position of button/ changing margin of button dynamically动态更改按钮的 X 和 Y 位置/更改按钮的边距
【发布时间】:2010-09-24 22:36:57
【问题描述】:

我想更改应用程序中按钮的边距... 假设我在线性布局中有 5 个这样的按钮,一个在 d 下方...

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

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

【问题讨论】:

    标签: android margin


    【解决方案1】:

    查看http://developer.android.com/reference/android/view/animation/Animation.htmlhttp://developer.android.com/reference/android/view/animation/ScaleAnimation.htmlAnimationScaleAnimation 课程。

    一种方法是在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 动画应该在您触摸按钮时设置。

    我希望这会有所帮助!

    【讨论】:

    • cos...确实有帮助..thanx 很多... :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多