首页 > 解决方案 > 按下按钮时如何增加按钮大小?请帮我

问题描述

在此处输入图像描述

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

<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" />

标签: android

解决方案


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

// 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

推荐阅读