这个问题在某种程度上是我上一个问题的延续。
我的问题现在基本相同,只不过我学到了一种方法,可以使用属性android:drawableLeft
为文本设置图像(Eclipse通过LinearLayout行上的警告图标向我提出了建议)。
我认为唯一的区别是,我将不再使用setImageResource()
方法来设置ImageView,而是使用setCompoundDrawablesWithIntrinsicBounds()
方法来设置TextView的drawableLeft
属性。但实际上,当我做出改变时,我重新遇到了最初的问题:文本与视图顶边对齐而不是居中对齐。
这是我的TextView的外观:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/account_login"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/pm_gmail"
android:drawablePadding="8dp"
android:text="example@gmail.com"
android:paddingLeft="16dp"
android:layout_gravity="left|center_vertical" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_centerVertical="true"
android:layout_below="@id/account_login"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:background="#DDDDDD" />
</RelativeLayout>
第二个View
只是一个分隔符。
... 设置了上述属性后,布局如下所示: (我还没有足够的声望来发布图片,所以这里是链接)
(只是为了明确起见,这只是一个静态示例。我的文本和图像均在运行时动态设置代码)。
非常感谢任何帮助。