如何在形状椭圆形按钮的按钮内添加图标

时间:2017-06-16 03:03:55

标签: android android-drawable android-button android-shape

我创建了一个可绘制的button_oval.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:shape="rectangle">

    <corners android:radius="120dp" />

    <solid android:color="#eceef5" />

    <stroke
        android:width="3dp"
        android:color="#29395e" />

    <size
        android:width="300dp"
        android:height="120dp" />

</shape>

然后我在我的布局上使用它:

// I want to add a icon inside this button
    <Button
        android:id="@+id/goToPersonalPage"
        android:layout_width="110dp"
        android:layout_height="50dp"
        android:layout_marginLeft="30dp"
        android:background="@drawable/button_oval"
        android:text="@string/memberButton"
        android:textColor="#29395e"
        android:textSize="18dp" />

我想在Button内添加一个图标,是否有可能通过更改我的button_oval.xml来完成它?

最终输出应该如下图所示:

enter image description here

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:7)

您必须在按钮元素

中添加这些行
android:drawableLeft="@mipmap/ic_launcher_round" // to set an icon
android:drawablePadding="10dp" // to set the padding of icon from text
android:paddingLeft="20dp"  // to set the padding of the icon and text

根据您的需要调整值。

看起来像 -

enter image description here

答案 1 :(得分:2)

Button元素

中添加此代码
 android:drawableLeft="@android:drawable/yourIcon"

答案 2 :(得分:1)

XML中的

android:drawableLeft="@drawable/button_icon"
android:drawablePadding="2dip"

或在Acitvity类

yourButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
yourButton.setCompoundDrawablePadding(padding_value);