【问题标题】:To increase a button's size when it is pressed in Android在Android中按下按钮时增加按钮的大小
【发布时间】:2020-11-19 19:55:14
【问题描述】:

如何使按钮的大小在按下时略微增大,在松开时再次减小?这是为了突出显示按下的按钮,除了使用不同的颜色外,还使用大小。

问候, 琪琪

【问题讨论】:

    标签: android button size pressed


    【解决方案1】:

    使您的Button 成为一个字段,并在与之关联的OnTouchListener 中修改其大小。使用 OnTouchListener 可以监听不同的MotionEvents,例如ACTION_DOWNACTION_UP

    【讨论】:

      【解决方案2】:
      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;
          }
      });
      

      【讨论】:

        【解决方案3】:

        自 API 21 (Lollipop) 起,基于 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>
        

        关于动画比例还有两个类似的问题:

        【讨论】:

        • 这第一个链接有最好的解决方案
        • 这个答案在我的项目中不起作用,直到我在默认项目中添加android:state_pressed="false"
        【解决方案4】:

        在你的活动中

        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);
                }
              });
            }
        

        【讨论】:

        • 但我的问题是在按下按钮时增加按钮大小并在释放按钮时恢复正常大小。
        • 然后使用 ontouch 监听器。在那你需要定义 Motionevent.On 行动起来。在行动下来。等等
        【解决方案5】:
        //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);
                }
              });
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-02
          • 1970-01-01
          • 1970-01-01
          • 2012-03-01
          • 2023-03-05
          • 1970-01-01
          • 2012-09-12
          • 1970-01-01
          相关资源
          最近更新 更多