java - Android背景+文本+按钮图标

标签 java android xml layout user-interface

我想将图像设置为背景,上面有文本,文本左侧有一个图标。 在 iPhone 中非常容易,但无法弄清楚如何在 Android 上执行此操作,调整按钮的大小并正确保持图标 + 文本的位置和距离。

苹果手机:

iPhone

Android 我有这个:

Android

xml代码为:

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

here 中获取代码.

如果我使用 android:drawableLeft ,图标将转到最左边的部分。

enter image description here

如果我开始使用半硬编码填充,那么我将对差异设备有不同的看法:(电话和表格)

如果我添加 android:gravity="left|center_vertical" 它将看起来像这样:

enter image description here

文本是可变的:当用户改变语言时它会改变。

如何正确地做到这一点?

我不想对任何人的回答投反对票,但请阅读问题并且不要建议我已经尝试过的内容。还被告知硬编码修复效果不佳。

这不是作业,而是商业软件部分。

这是答案中的建议代码:

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

您认为 Galaxy s4 上的 android:layout_marginLeft="10dip" 会是什么样子?这是预览:

enter image description here

这不是我问的。 “dip”或“dp”或“px”不应在任何地方用作距左上角的距离,因为手机具有 HPDI、较小的屏幕,而平板电脑具有 MDPI 和宽分辨率。 Simple 不适用于 mdpi 和 xxhdpi。

Nizam answer非常接近一个好的解决方案:

enter image description here

最佳答案

也许您应该使用带圆角的 RelativeLayout,而不是将 TextView 和 ImageView 放在其中。

关于java - Android背景+文本+按钮图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19133544/

相关文章:

java - 使用 Volley 的 JSON 请求

java - Java 中的分词器、停用词删除、词干提取

java - 自实例化机制/概念在 Java 中如何工作?

java - 使用摇一摇功能打开不同的 Activity

java - 在 Java 8 中使用流、lambda

android - 在应用程序的任何 Activity 中调用 onPause() 时通知

java - 将 onclick 监听器添加到 fragment 的 xml 中的按钮

java - XMLEventWriter 不写入输出流

xml - 如何通过 XSLT 转换选择 XML 中的特定节点或元素?

java - 在不将所有比较数据加载到内存的情况下比较两组 XML 数据