如何在同一按钮中对齐文本和图像

时间:2013-01-11 07:37:01

标签: android

如何使用相同的按钮对齐文本和图像?我想这样做:

enter image description here

但我的按钮看起来像这样,文字与图像重叠:

enter image description here

这是我的代码如下。如何在同一个按钮中在文本和图像之间留出空间?

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_weight="0"
    android:weightSum="1"
    android:orientation="horizontal" >

       <Button android:id="@+id/btnUtilitybillpayments"
            style="?android:attr/buttonStyleSmall"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:drawableLeft="@drawable/handshake"
            android:layout_marginRight="55dip"
            android:layout_marginLeft="55dip"
            android:drawablePadding="-25dp"
            android:paddingLeft="10dip"
            android:paddingRight="50dip"
            android:layout_marginTop="10dip"
            android:background="@drawable/curvedplanebutton"
            android:textColor="@drawable/button_text_color"
            android:text="Utility Bills Payment"/>
</LinearLayout>

4 个答案:

答案 0 :(得分:1)

我认为你按下了Complex,

只需使用以下代码,让我知道发生了什么......

<Button   android:id="@+id/btnUtilitybillpayments"
             style="?android:attr/buttonStyleSmall"
             android:layout_height="wrap_content"
             android:layout_width="0dp"
             android:layout_weight="1"
             android:layout_marginRight="55dip"
             android:layout_marginLeft="55dip"
             android:drawableLeft="@drawable/handshake"
             android:layout_marginTop="10dip"
             android:gravity="left"
             android:paddingLeft="20dip"
             android:background="@drawable/curvedplanebutton"
             android:textColor="@drawable/button_text_color"
             android:text="Utility Bills Payment"/>

我还怀疑你的Button的layout_margin属性。如果它不起作用,请尝试删除

 android:layout_marginRight="55dip"
 android:layout_marginLeft="55dip"

答案 1 :(得分:1)

试试这个

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:drawableLeft="@android:drawable/btn_star_big_on"
 android:text="Drawable left">
Button>

For more info go to this link

http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx

答案 2 :(得分:0)

有点像这样。

        <Button
            android:id="@+id/btnUtilitybillpayments"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"

            android:layout_weight="1"
            android:background="@drawable/curvedplanebutton"
            android:drawableLeft="@drawable/handshake"
            android:gravity="left|center"
            android:text="Utility Bills Payment"
            android:textColor="@drawable/button_text_color" />

答案 3 :(得分:0)

更合适的方法是将按钮设为9patch图像。这样,您可以通过修补右侧和底侧来定义文本的位置。而且不需要这么复杂的按钮。

您可以到此处获取更多信息 -

http://developer.android.com/tools/help/draw9patch.html