Android的背景+文字+图标按钮 [英] Android background + text + icon for a button

查看:216
本文介绍了Android的背景+文字+图标按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有设置为背景上的文本和文本的左侧的图标图像。 在iPhone很容易,但无法弄清楚如何做到这一点的机器人,是可调整大小的按钮并保持图标+文字位置和距离正常。

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:

Android的我有这样的:

Android I have this:

在XML code是:

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

从<一个拿过code href="http://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton">here.

如果我用安卓drawableLeft ,比该图标将进入最左侧部分。

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

如果我开始与半硬codeD播放垫,比我都会有不同的看差异devives:(电话表)

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

如果我添加了安卓重力=左| center_vertical比它看起来是这样的:

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

的文本是可变的:它会发生变化,当用户改变语言

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

如何正确地做到这一点?

How to do it properly?

我不希望downvote任何人的答案,但请阅读问题,不建议我已经tryed。还告诉硬codeD补丁不能很好地工作。

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.

下面是答案,建议code:

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>

你觉得它会是什么样子的安卓layout_marginLeft =10dip银河S4?这里是preVIEW:

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

这是不是我所问。 畅游或DP或PX不应被任何地方使用来自左侧,顶部的距离,因为手机有HPDI,较小的屏幕和平板电脑有MDPI和广泛的决议。简单上不MDPI,和xxhdpi工作

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:

推荐答案

也许你应该使用RelativeLayout的带圆角的,不是把TextView的和ImageView的它的内部。

Maybe you should use RelativeLayout with rounded corners, than put TextView and ImageView inside of it.

这篇关于Android的背景+文字+图标按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆