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

标签 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

如有任何帮助,我们将不胜感激。

最佳答案

您必须在 Button 元素中添加这些行 -

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

关于android - 如何在形状为椭圆形按钮的按钮内添加图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580113/

相关文章:

android - 如何在 Android Studio 中制作选择器

android - 底部工作表按钮自定义样式不起作用

android - 如何设置 JetpackCompose 按钮​​背景颜色

java - 取消选择 ListView 中的项目

iphone - 是否有任何通用语言可以为 iphone、blackberry、windows mobile 和 android 开发相同的应用程序?

android - 如何向按钮添加边框和背景图像

android - 为 ListView 中的每个元素设置 backgroundDrawable 时的 SIGSEGV

Android 在运行时抛出异常时询问权限

android - 如何在 Android 自定义 Canvas 中绘制圆形?

安卓 : Create a button with border bottom