首页 > 解决方案 > Android中的圆角按钮不扩大尺寸

问题描述

这篇文章有很多解释如何在 Android 中创建圆形按钮的答案。但几乎所有答案最终都会扩大按钮的大小。

例如,在此布局中,请注意顶部按钮(带有圆角的按钮)的填充比其他按钮多得多。

截屏

这是没有圆角的按钮的代码;

<Button
    android:id="@+id/useKeyboardButton"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/use_keyboard_msg"
    app:layout_constraintBottom_toTopOf="@+id/privacyPolicyButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/enableKeyboardButton"
    style="@style/Widget.AppCompat.Button.Colored"
    />

这是圆角按钮的代码:

<Button
    android:id="@+id/enableKeyboardButton"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/enable_keyboard_msg"
    app:layout_constraintBottom_toTopOf="@+id/useKeyboardButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    style="@style/Widget.AppCompat.Button.Colored"
    android:background="@drawable/rounded_corners"
    />

背景是这个可绘制的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorAccent"/>
    <corners android:radius="10dp"/>
</shape>

标签: androidandroid-drawableandroid-buttonrounded-cornersandroid-background

解决方案


尝试删除样式属性并仅设置背景可绘制对象。


推荐阅读