[英]Image + Text to be centre alligned in button
I have a Button with its width as match_parent
. 我有一个按钮,其宽度为match_parent
。 And I want a image/icon with a text in its center. 我想要一个图像/图标,中间带有文本。 I have tried android:drawableStart
attribute but its of no help. 我尝试了android:drawableStart
属性,但没有帮助。 I don't know what I am missing. 我不知道我在想什么。 Is there any one who is also facing the same problem. 是否有人也面临同样的问题。
Note: This link is not solving my problem. 注意: 此链接不能解决我的问题。 I am attaching my button portion of xml 我要附加我的xml按钮部分
<Button
android:background="@drawable/blue_button"
android:id="@+id/btn_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:drawableStart="@drawable/ic_list"
android:text="@string/btn_schedule"
android:textColor="@android:color/white" />
After adding these attribute my text is coming on center but image/icon is still on left side of button. 添加这些属性后,我的文本位于中间,但图像/图标仍位于按钮的左侧。 Can any body help!!! 任何身体都可以帮忙!!!
You might want to check my answer to this question: Android Button with text and image 您可能需要检查一下我对以下问题的答案: 带有文本和图像的Android按钮
Its an ugly solution but it you want the image and text to be centered as a group in the button rather than text center and drawable padded from the side then its the only way I have found. 这是一个丑陋的解决方案,但您希望图像和文本作为一个组在按钮中居中,而不是在文本中心居中,并从侧面填充图形,这是我找到的唯一方法。
I tried android:drawableTop
我尝试了android:drawableTop
The image shown in the center of the button 该图像显示在按钮中央
I had the same problem and I hadn't found anywhere clear solution. 我遇到了同样的问题,却找不到任何明确的解决方案。 BUT I got some information which also help me to provide following solutions (I have chosen the second one): 但是我得到了一些信息,这些信息也有助于我提供以下解决方案(我选择了第二种解决方案):
Create image which will contain your image and text 创建将包含您的图像和文本的图像
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableTop="@drawable/YOUR_IMAGE" android:gravity="center_horizontal|center_vertical" />
It is not so clear and you have to add more complicated changes to your layout but it is easier to modify the text later 不清楚,您必须在布局中添加更复杂的更改,但是以后修改文本更容易
<FrameLayout android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_height="50dp" android:layout_width="match_parent" /> <TextView android:text="sample" android:layout_height="50dp" android:layout_width="wrap_content" android:drawableLeft="@drawable/YOUR_IMAGE" android:gravity="center_horizontal|center_vertical" /> </FrameLayout>
100% working 100%工作
<FrameLayout
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:clickable="false"
android:drawableLeft="@android:drawable/ic_delete"
android:focusable="false"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="Button Challenge" />
This works for me. 这对我有用。 Basically play with the paddingLeft and PaddingRight of the button to get the text and image close together to the center. 基本上使用按钮的paddingLeft和PaddingRight可以使文本和图像靠近中心。
Note: Might be an issue with rather long texts. 注意:较长的文字可能会成为问题。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="search button"
android:drawableStart="@drawable/ic_action_search"
android:paddingLeft="60dp"
android:paddingRight="60dp"/>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.