在按钮内设置可绘制位置

28

我有一个按钮和一个文本左侧的可绘制对象,但我希望该可绘制对象更靠近文本。 所以我需要移动可绘制对象。

我已经定义了android:drawableLeft,但是按钮的内容并没有居中。

这是我的代码:

<Button
    android:id="@+id/addsubject"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_action_add"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:text="@string/addsubject"
    android:textColor="@color/white"
    android:background="@drawable/custombutton1" />

现在的样子如下:

输入图像描述

我希望它变成这个样子:

输入图像描述

谢谢!


尝试在xml中添加 android:gravity="center" - suitianshi
请参考以下链接:https://dev59.com/3XA65IYBdhLWcg3wyRyk 。 - CoolMind
8个回答

20

MaterialButton中使用app:iconGravity="textStart"属性。

    <com.google.android.material.button.MaterialButton
        app:icon="@drawable/ic_add_24px"
        app:iconGravity="textStart"
        ../>

输入图像描述

如果您想减少图标和文本之间的填充,请使用app:iconPadding属性:

    <com.google.android.material.button.MaterialButton
        app:icon="@drawable/ic_add_24px"
        app:iconGravity="textStart"
        app:iconPadding="0dp"/>

输入图像描述在此


除非您像这样将Material组件主题应用于按钮android:theme="@style/Theme.MaterialComponents.Light",否则您的答案将无法正常工作。 - Michael

18

试试这个

 <Button
    android:id="@+id/addsubject"
    android:layout_width="160dip"
    android:layout_height="60dip"
    android:layout_gravity="center"
    android:drawableLeft="@drawable/ic_action_add"
    android:drawablePadding="2dip"
    android:gravity="center"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:singleLine="true"
    android:text="@string/addsubject"
    android:textSize="13dip" />

1
这正是我在寻找的!诀窍就是使用drawableLeft和drawableRight,并调整它们以适应。谢谢! - Isaías
1
它可以工作,但是根据设备尺寸不够灵活。 - BlackBlind

6
我只需将这些属性专门应用于按钮,就可以得到此结果。
<Button
    ...
    android:paddingLeft="60dp"
    android:drawablePadding="-50dp"
    android:gravity="center"
    android:drawableLeft="@drawable/your_button_icon"
    /> 

您可以使用自己的值来调整以获得所需的结果。
干杯 :-)

谢谢,它有效。但是如果您有android:padding,请删除它。否则,图标将位于左侧,文本将居中。此外,结果取决于按钮的宽度。 - CoolMind
解决方案不好,因为这样按钮在不同的屏幕尺寸上看起来会有所不同。 - dephinera
@definera,您能否提出一个更好的解决方案,使此视图(按钮)可以适应不同的屏幕尺寸? - Imtiaz Abir
1
@ImtiazAbir 或许使用带有ImageSpan的SpannableString是一个选项。个人还没有尝试过,所以无法给出具体示例。 - dephinera

3

我只是简单地使用了paddingLeft和paddingRight。它有效果!...android:paddingLeft="25dp" android:paddingRight="25dp"/>


1
按钮左边框与图标右边框之间的填充由android:paddingLeft属性控制。图标和文本之间的填充由android:drawablePadding属性定义。
将这两个属性添加到您的按钮中,并设置您满意的值。
<Button
    ...
    android:paddingLeft="24dp"
    android:drawablePadding="8dp"
    />

1

您可以通过添加 OnClickListener 并自定义 LinearLayout 来将其用作按钮。

<LinearLayout          
    android:layout_width="160dip"
    android:layout_height="60dip"
    android:orientation="horizontal" 
    android:background="@drawable/custombutton1">

    <ImageView android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:src="@drawable/ic_action_add"
               android:layout_gravity="center"
               android:layout_weight="1"
               android:scaleType="center">

    </ImageView>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/addsubject"
              android:layout_gravity="center"
              android:layout_weight="1"/>

</LinearLayout>

0

0
对于 Materialbutton,您需要使用 icon 属性,并可以像以下示例一样使用 iconTint 更改其色调:
<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:icon="@drawable/ic_cancel"
    app:iconTint="@color/black"
    android:text="text" />

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