Android:减小按钮的大小

36

我在一个布局中使用了Button。我希望Button的大小与文本/标签的大小一样小。

我正在使用以下代码。

<Button 
android:text="@string/cancle_button" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:textColor="#ffffff"
android:textStyle="bold" />

但是我希望按钮的高度与文本/标签的高度相同。每当我减小或增加文本高度时,按钮的高度保持不变。我该如何解决这个问题?


2
由于您正在将图像放在背景中,因此按钮大小将取决于图像大小和文本大小。因此,如果您的文本大小小于图像,则不会减小。但是,如果文本大小大于图像,则会增大以适应文本。这一切都是因为“wrap_content”属性。 - arshu
1
谢谢Arshad,但是我在我的drawable文件夹中使用渐变可绘制的.xml形式。 - Swapnil Desai
即使如此,梯度也具有固定的大小和形状。因此,它就像图像本身一样工作。 - arshu
最好的做法是根据文本大小调整渐变大小以完成您的工作。 - arshu
7个回答

187

这个问题已经存在了将近一年。但我现在遇到了这个问题并找到了解决方法。 :) 也许能帮到其他人。

您可以通过为Button小部件设置android:minHeight="0dp"来实现此目标。

我猜测这种行为是因为框架为Button小部件设置了一些默认值(也许是48dp),考虑到最小触摸区域。


3

在这种情况下,您可以使用图像视图或文本视图。如果您选择使用图像视图,则应确保将其设置为适当的大小,以避免模糊。通过这些措施,您可以为图像设置较小的尺寸。最好的方法是使用您想要创建的尺寸来获取图像。


在这个需求中,我正在使用渐变形状可绘制对象在我的可绘制文件夹中。 - Swapnil Desai

1
通过将按钮高度设置为与标签相同的特定值,您可以解决此问题。
<Button 
android:text="CANCEL" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content"
android:layout_height="20sp"
android:gravity="center"
android:textSize="15sp"
android:textColor="#ffffff"
android:textStyle="bold" />

1
谢谢回复,有没有其他方法可以使按钮在不硬编码的情况下自动调整大小。我希望当文本大小改变时,按钮大小也会改变。 - Swapnil Desai
如果您更改文本的大小,由于高度设置为自动调整内容,它将自动调整该高度。 - jyomin
按钮高度没有调整,仍然保持不变。 - Swapnil Desai
你只需要将按钮的高度设置为wrap content,并改变文本的大小,它就会自动调整。 <Button android:text="取消" android:id="@+id/button1" android:background="@drawable/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textSize="125sp" android:textColor="#ffffff" android:textStyle="bold" /> - jyomin

1
你可以使用相对布局来实现这个功能。请查看以下示例代码:
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textView1"
        android:layout_alignTop="@id/textView1"
        android:layout_alignBottom="@id/textView1"
        android:text="Button" />

</RelativeLayout>

0

您可以通过更改按钮的 layout_weight 来解决此问题,因为默认情况下它设置为“1”

只需将其设置为0,然后通过更改按钮的 layout_height 来更改 button_height。 以下是普通按钮的代码-

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Button" />

0

你可以使用

android:minHeight="0dp" -> 将按钮高度减小到标签大小

android:minWidth="0dp" -> 将按钮宽度减小到标签大小

所以;

<Button 
    android:text="@string/cancle_button"
    android:minHeight="0dp"
    android:minWidth="0dp" 
    android:id="@+id/button1"
    android:background="@drawable/back_button" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="12sp"
    android:textColor="#ffffff"
    android:textStyle="bold" />

-1
        <com.google.android.material.button.MaterialButton
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/custom_small_button_bg"
            android:drawableLeft="@drawable/ic_minus"
            android:paddingStart="6dp"
            android:paddingLeft="6dp" />

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