Android background + text + icon for a button

2019-03-25 14:17发布

I would like to have an image set to background a text on it and an icon on the left side of the text. Very easy in iPhone, but can't figure out how to do it at Android, to be resizable that button and keep the icon + text position and distance properly.

iPhone:

iPhone

Android I have this:

Android

The xml code is:

<Button
    android:id="@+id/btSettings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/tvWhatever"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"
    android:background="@drawable/bt_general"
    android:drawableTop="@drawable/settings_selected"
    android:text="@string/settings"
    android:textColor="#000000" />

Took the code from here.

If I use android:drawableLeft , than the icon will go to most left part.

enter image description here

If I start playing with semi hardcoded paddings, than I will have different look at diff devives: ( phone and table)

If I add the android:gravity="left|center_vertical" than it will look like this:

enter image description here

The text is variable: it will change, when the user change the language.

How to do it properly?

I don't want to downvote anybody's answer, but please read the question and don't suggest what I have tryed already. Also told the hardcoded fixes doesn't work well.

This is not a homework, but a commercial software part.

Here is a suggested code from answers:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bt_general"
        android:padding="20dip" >

        <ImageView
            android:id="@+id/xIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dip"
            android:src="@drawable/settings_selected" />

        <TextView
            android:id="@+id/xSettingsTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/settings"
            android:textColor="#000000" />
    </RelativeLayout>

What do you think how it will look like the android:layout_marginLeft="10dip" on Galaxy s4? Here is a preview:

enter image description here

This is not, what I have asked. "dip" or "dp" or "px" shouldn't be used anywhere as distance from left, top, because phones has HPDI, smaller screen and Tablets has MDPI and wide resolutions. Simple doesn't work on mdpi, and xxhdpi.

Nizam answer is very close to a good solution:

enter image description here

14条回答
疯言疯语
2楼-- · 2019-03-25 15:09

You can use android:paddingLeft, android:paddingRight and layout_width at a fixed width to solve the issue.

<Button
android:id="@+id/btSettings"
android:layout_width="148dp"
android:layout_height="wrap_content"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:background="@drawable/bt_general"
android:drawableLeft="@drawable/settings_selected"
android:text="@string/settings"
android:textColor="#000000"/>  
查看更多
登录 后发表回答