简体   繁体   中英

A button with an image/text centered in background

I have seen a lot of possible answers to my problem but none of them work for me. My goal is to have a rounded button with an image, some text and a background. The image and text should be centered in the background.

I'm using the following for the background:

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

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

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

    <size
        android:width="120sp"
        android:height="36sp" />

</shape>

The following for the image:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24sp"
    android:height="24sp"
    android:viewportWidth="24"
    android:viewportHeight="24">
  <path
      android:pathData="M5,12L19,12"
      android:strokeLineJoin="round"
      android:strokeWidth="2"
      android:fillColor="#00000000"
      android:strokeColor="#FFFFFF"
      android:strokeLineCap="round"/>
  <path
      android:pathData="M12,5L12,19"
      android:strokeLineJoin="round"
      android:strokeWidth="2"
      android:fillColor="#00000000"
      android:strokeColor="#FFFFFF"
      android:strokeLineCap="round"/>
</vector>

And finally the button:

    <Button android:layout_columnSpan="6" 
android:layout_gravity="center" android:drawableStart="@drawable/ic_plus_24px" 
android:text="Evaluer" 
android:id="@+id/doEvalQOL" android:background="@drawable/roundedyakabutton" android:textColor="#FFFFFF" 
android:gravity="center" 
android:textSize="12sp" 
fontPath="fonts/Ubuntu-Medium.ttf" />

This gives me this: 在此处输入图片说明

As soon as I remove the background, the image and text are placed as I would like them to be with the background:

在此处输入图片说明

What am I missing here ? Thanks in advance.

You can use drawablePadding, paddingLeft and paddingRight to align text and its icon in the center of the button. I have used below code and the result is attached as a screenshot. Try once hope it helps you.

    <Button
    android:id="@+id/doEvalQOL"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/roundedyakabutton"
    android:drawableLeft="@drawable/ic_plus_24px"
    android:text="Evaluer"
    android:drawablePadding="2dip"
    android:gravity="center"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:singleLine="true"
    android:textColor="#FFFFFF"
    android:textSize="12sp"
     />

上面按钮的输出

Problem

You can not set gravity of drawableLeft in Button/ TextView. Here you have set gravity to Button, that makes text center but image keeps in left.

Solution

  • Use minus margin android:drawablePadding="-20sp" for drawableLeft .
  • Or don't use ImageView and Button wrapping with parent View (with background) instead of Button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM