TextView中文本的偏心对齐

时间:2013-02-14 13:11:33

标签: android layout textview alignment

我有一个TextView的布局/对齐问题,我还没有找到解决方案,也就是说,我想在TextView水平方向偏离中心对齐文本。

为了提供更多的上下文,我有一个ImageView和一个TextView并排,ImageView触摸屏幕的左边缘,TextView水平填充屏幕的其余部分,如下所示:

[-img-|-----text-----]

TextView配置为singleLine =“true”和maxLines =“1”,因此如果它的水平空间太长,它将被截断。我的目标是将文本对齐在屏幕的中心,而不是TextView的中心,因为屏幕上还有其他元素与中心对齐,我需要文本对齐以匹配它。

所以,如果我在TextView上使用gravity =“center”,我会得到上面的图片,但我真正想要的是

[-img-|--text--------]

我尝试将图像和文本放在RelativeLayout中,这样TextView实际上触及了屏幕的两个边缘,这就做了我想要的对齐,除了如果文本足够长,第一个字符将是由图像隐藏,因为TextView位于图像视图的后面。

我还尝试了左边的边距,填充和复合绘制,但文本总是相对于可用空间居中(我会考虑预期的行为)。

有没有人有关于如何实现这种对齐的线索,即相对于TextView不同组件的中心,可能是在运行时以编程方式?提前感谢任何有用的建议。

编辑:用户Budius建议使用右边的填充来实现居中对齐,这会导致长文本在到达TextView的右边缘之前被截断并且我正在寻找避免这种情况的解决方案,即如果可能的话,使用整个可用空间。

3 个答案:

答案 0 :(得分:2)

我相信这会使它居中并且向前移动20dp。

gravity:centre padding:right=20dp

答案 1 :(得分:0)

尝试使用

   android:layout_weight="1" 

如果它对你有帮助..

答案 2 :(得分:0)

我知道这不是正确的方法而不是试一试..

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight=".33"
    android:src="@drawable/ic_launcher" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight=".33"
    android:gravity="center"
    android:text="text" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight=".33"
    android:src="@drawable/ic_launcher"
    android:visibility="invisible" />

相关问题