如何将TextView中的文本与Compound Drawable垂直对齐

Nic*_*lli 11 layout android textview android-layout compound-drawables

这个问题在某种程度上延续了我的上一个问题.

我现在的问题几乎是一样的,除了不是在不同视图中分离图像和文本(即ImageView和TextView),我学会了我可以使用该属性android:drawableLeft为我的文本设置一个图像(建议指向我由Eclipse在LinearLayout行上带有警告图标).

我认为唯一的区别是,而不是与设置ImageView的setImageResource()方法,我想简单地设置的TextView的drawableLeft与归因setCompoundDrawablesWithIntrinsicBounds()方法.相反,当我做出改变时,我回到了原来的问题:文本与视图的上边缘而不是中心对齐.

这就是我的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>
Run Code Online (Sandbox Code Playgroud)

第二个View只是一个分隔符.

...这就是设置上述属性后布局的样子:(我没有足够的声誉来发布图片,所以这里是它的链接)

(为了清楚起见,这只是一个静态示例.我的文本和图像都是在运行时在代码中动态设置的).

任何帮助是极大的赞赏.

Fan*_*mas 18

更改android:layout_gravity="left|center_vertical"android:gravity="center_vertical".
layout_gravity用于将View定位在容器(布局)中,同时gravity引用View内容(即,在本例中为TextView内的文本).

  • 我们在这里互相帮助.也许,有一天,我会有一个问题,你会得到答案. (6认同)
  • @BobMalooga 当你选择 left 时,它会在左侧垂直“居中” - 9 点钟方向。不幸的是,要求是图标浮动到顶部,与顶部的文本对齐,而不是在文本前面居中。我试图使用指向 &lt;bitmapgravity="something" src=realdrawable.png/&gt; 的可绘制对象,但它似乎不起作用。 (2认同)