[英]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" />
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. 您可以使用drawablePadding,paddingLeft和paddingRight在按钮中心对齐文本及其图标。 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. 您不能在Button / TextView中设置
drawableLeft
gravity
。 Here you have set gravity to Button, that makes text center but image keeps in left. 在这里,将重力设置为“按钮”,使文本居中,但图像保持在左侧。
Solution 解
android:drawablePadding="-20sp"
for drawableLeft
. android:drawablePadding="-20sp"
用于drawableLeft
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.