图像+文本在按钮中居中对齐

时间:2013-07-08 05:54:50

标签: android android-button

我有一个宽度为match_parent的按钮。我想要一个带有文本的图像/图标。我尝试了android:drawableStart属性,但没有帮助。我不知道我错过了什么。有没有人也面临同样的问题。

注意:This link无法解决我的问题。我附加了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" />

添加这些属性后,我的文字会出现在中心,但图像/图标仍在按钮的左侧。任何人都可以帮忙!!!

5 个答案:

答案 0 :(得分:1)

您可能想查看我对此问题的回答: Android Button with text and image

它是一个丑陋的解决方案,但你希望图像和文本在按钮中居中作为一组而不是文本中心和从侧面填充的可绘制区域,然后是我找到的唯一方法。

答案 1 :(得分:0)

我尝试了android:drawableTop

按钮中央显示的图像

答案 2 :(得分:0)

我有同样的问题,我没有找到任何明确的解决方案。但我得到了一些信息,这也有助于我提供以下解决方案(我选择了第二个):

  1. 创建包含图片和文字的图片

     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:drawableTop="@drawable/YOUR_IMAGE"
         android:gravity="center_horizontal|center_vertical" />
    
  2. 目前还不是很清楚,您必须在布局中添加更复杂的更改,但以后修改文本会更容易

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

答案 3 :(得分:0)

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" />

来自here

答案 4 :(得分:0)

这对我有用。基本上使用按钮的paddingLeft和PaddingRight来使文本和图像靠近中心。

注意:可能会出现相当长的文本问题。

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