如何有图片和一个按钮中的文本中心 [英] How to have Image and Text Center within a Button
问题描述
我要显示文本
和图标
上的按钮。
I want to display TEXT
and Icon
on a Button.
+----------------------------+
| Icon TEXT |
+----------------------------+
我试着用
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="40dip"
android:text="TEXT"
android:drawableLeft="@drawable/Icon" />
但文本
和图标
不在中心。
我的文本
大小各不相同,根据文本尺寸图标
和文本
应该得到调整为中心。
But Text
and Icon
is not in center.
My Text
size varies, according to text size Icon
and Text
should get adjusted to center.
我应该怎么办呢?
推荐答案
您可以通过一个更复杂的布局,但我不知道它是否是值得的,假的吧。这里的东西我砍死在一起:
You can fake it by making a more complex layout, but I'm not sure whether it's worth it. Here's something I hacked together:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignTop="@+id/foreground"
android:layout_alignBottom="@id/foreground"
android:layout_alignRight="@id/foreground"
android:layout_alignLeft="@id/foreground"
android:onClick="clickedMe" />
<RelativeLayout
android:id="@id/foreground"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/button_text"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:src="@drawable/icon" />
</RelativeLayout>
</RelativeLayout>
有可能是一个更简洁的方式来做到这一点。我倾向于努力让RelativeLayout的做我想做的时候。请注意,您需要注意的Z轴顺序(按钮需要先在顶层RelativeLayout的出现),你可能需要调整填充得到它看起来你想要的方式。
There might be a more concise way to do it. I tend to struggle getting RelativeLayout to do what I want sometimes. Note that you need to pay attention to the z-order (Button needs to appear first in the top level RelativeLayout) and you might need to adjust padding to get it to look the way you want.
这篇关于如何有图片和一个按钮中的文本中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!