保持文字的TextView与drawableLeft中心 [英] Keep text in TextView with drawableLeft centered

查看:132
本文介绍了保持文字的TextView与drawableLeft中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序我有一个由具有FILL_PARENT宽度,其中有一个特定的背景颜色和一些文字为中心的单一的TextView的标题栏。现在,我想添加一个可绘制在标题栏的左边,所以我设置drawableLeft果然文本和图像显示。但这里的问题是,该文本不再正确居中,例如,当绘制添加文本转移有点向右如这里的截图:

In my app I have a header bar which consists of a single textview with fill_parent as width, which have a specific background color and some centered text. Now I want to add a drawable on the left side of the header bar, so I set the drawableLeft and sure enough the text and image is displayed. However the problem here is that the text is no longer properly centered, e.g., when the drawable is added the text is shifted a bit to the right as shown in the screenshots here:



反正我有可以正常文本居中,并有被​​拉伸的位置,因为它是以上无需使用额外的版面项目(如LinearLayout中)?

Is there anyway I can center the text properly and have the drawable positioned as it is above without using an additional layout item (such as a LinearLayout)?

推荐答案

,以获取最简单的方法是:

The simplest way to obtain that is:

<RelativeLayout
         android:id="@+id/linearLayout1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/head" >

         <ImageView
             android:id="@+id/cat"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="5dp"
             android:onClick="onClick"
             android:layout_marginTop="10dp"
             android:layout_alignParentLeft="true"
             android:src="@drawable/btn_back_d" />

       <TextView
            android:id="@+id/TvTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text=""
            android:layout_centerHorizontal="true" 
            android:textColor="#000000" 
            android:textSize="20sp"/>

其中,

  • 的ImageView有alignParentLeft设置好的真实
  • 的TextView有centerHorizo​​ntal设置好的真

这篇关于保持文字的TextView与drawableLeft中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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