在Android中,当按钮被按下时增加其大小

5

我怎么能让按钮在按下时略微增大,在释放时再缩小呢?这是为了通过使用大小和不同的颜色来突出显示按下的按钮。

问候, Kiki

5个回答

3
将您的按钮变成一个字段,并在与其相关联的OnTouchListener中修改其大小。使用OnTouchListener,您可以监听不同的MotionEvents,例如ACTION_DOWN和ACTION_UP。

2
自 API 21(棒棒糖)起,基于 stateListAnimator 属性的仅限 XML 解决方案可用。尽管其主要目标是根据状态更改动画视图属性,但如果需要,您可以通过设置 duration="0" 来禁用动画。例如:
<!-- res/layout/my_layout.xml -->

<LinearLayout ...
  android:stateListAnimator="@animator/zoom_animator">...</layout>

<!-- res/animator/zoom_animator -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <set>
      <objectAnimator android:propertyName="scaleX" android:valueTo="1.0" android:duration="0" />
      <objectAnimator android:propertyName="scaleY" android:valueTo="1.0" android:duration="0" />
    </set>
  </item>
  <item android:state_pressed="true">
    <set>
      <objectAnimator android:propertyName="scaleX" android:valueTo="1.1" android:duration="0" />
      <objectAnimator android:propertyName="scaleY" android:valueTo="1.1" android:duration="0" />
    </set>
  </item>
</selector>

还有两个关于动画缩放的类似问题:


这个第一个链接有最好的解决方案。 - Mohit Rajput
https://github.com/material-components/material-components-android-examples - Mohit Rajput
1
在我的项目中,除非我在默认项中添加 android:state_pressed="false",否则这个答案不起作用。 - Saint

1
button.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();

        //Code to convert height and width in dp.
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, getResources().getDisplayMetrics());
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, getResources().getDisplayMetrics());

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            layoutParams.width = width + 5;
            layoutParams.height = height + 5;
            SearchButton.setLayoutParams(layoutParams);
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            layoutParams.width = width;
            layoutParams.height = height;
            SearchButton.setLayoutParams(layoutParams);
        }

        return false;
    }
});

-1

在你的活动中

Private Button myButton;

//...

protected void onCreate(Bundle savedInstanceState) {

//...

myButton = (Button) findViewById(R.id.my_button);
    _button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        myButton.setWidth(50);
        myButton.setHeight(50);
        }
      });
    }

但我的问题是在按下按钮时增加按钮大小,并在释放按钮时恢复正常大小。 - kiki
那么就使用一个ontouch监听器。在其中你需要定义Motionevent.On action up、On action down等。 - Abhishek Susarla

-1
//Try it

Private Button myButton;

//...

protected void onCreate(Bundle savedInstanceState) {

//...

myButton = (Button) findViewById(R.id.my_button);
    _button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        myButton.setWidth(myButton.getWidth()+5);
        myButton.setHeight(myButton.getHeight()+5);
        }
      });
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接