【问题标题】:How to increase the button size when the botton was press? please help me按下按钮时如何增加按钮大小?请帮我
【发布时间】:2021-03-30 05:06:48
【问题描述】:

请帮我这样做,当我按下按钮时,按钮大小会增加。我会给截图。我也会分享一些按钮的代码

<Button
    android:id="@+id/sbi_button"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="40dp"
    android:backgroundTint="#1B79E6"
    android:gravity="center"
    android:insetLeft="0dp"
    android:insetTop="0dp"
    android:insetRight="0dp"
    android:insetBottom="0dp"
    android:padding="12dp"
    app:icon="@drawable/ic_sbi"
    app:iconPadding="0dp"
    app:iconSize="17dp"
    app:iconTint="#FFFFFF"
    app:layout_constraintEnd_toStartOf="@id/axis_button"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@id/transfer_textView"
    app:rippleColor="@android:color/transparent"
    app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Circle"
    app:strokeWidth="0dp" />

【问题讨论】:

  • 而不是截图给出一些代码你如何处理这个按钮
  • 请看我将分享代码
  • 你可能想用动画改变状态,否则看起来会很奇怪。看看This thread ..它的作用相反,你需要稍微调整一下。

标签: android


【解决方案1】:

您可以使用LayoutParams 更改Button(或任何其他View)的大小

// obtaining reference to view, e.g. in Activity
Button b = findViewById(R.id.sbi_button); 

// setting new size
int newSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60,
            getResources().getDisplayMetrics()); // 60dp to px unit
b.getLayoutParams().width = newSize; // setting new width and height
b.getLayoutParams().height = newSize;
b.requestLayout(); // force remeasure and redraw, may not be needed

【讨论】:

  • 你是对的 requestLayout() 这里不需要,你已经改变了布局参数。这样就可以了。可能删除它使用requestLayout IMo 不是一个好习惯
  • 确切地说不会这样做。为LayoutParams 设置新的width 只是将新值附加到存储在此类中的一些原始变量。框架经常管理布局和重新测量/刷新,但并非总是如此,不定期。在某些不需要重绘框架的情况下,它不会强制重新测量此Button,即使使用新尺寸,此View 也会保持不变,直到其他一些动作强制重新测量布局,这可能需要几秒钟。但我同意,在大多数情况下不需要requestLayout()
猜你喜欢
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
相关资源
最近更新 更多