在Android中如何使文本居中显示在一个狭窄的TextView?

4
我有一个高度为30px、文本大小为40px的TextView。由于文本比View更高,我只想显示文本的中间部分。如此:enter image description here 但是通过android:gravity="center_vertical",我只能显示文本的上部分,并截掉一些底部部分。enter image description here 这是我的代码:
 <TextView
         android:layout_width="wrap_content"
         android:layout_height="30px"
         android:textSize="40px"
         android:text="ABCDEFG" 
         android:gravity="center_vertical"
         />

有人知道如何做到这一点吗?谢谢!

(针对IT技术问题的询问)

1
请发布您的代码,并附上布局图像。 - Amitabh Sarkar
这里出现了 XML 元素: <TextView android:layout_width="wrap_content" android:layout_height="30px" android:textSize="40px" android:text="ABCDEFG" android:gravity="center_vertical" /> 就是这样了。 - user2194312
2个回答

2

你可以将它简单地放入一个适当大小的线性视图中,并调整文本视图内的边距到字体大小的一半左右(根据填充情况而定)。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="@android:color/white">

    <TextView
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Sliced in half"
        android:textSize="60dp"
        android:layout_marginTop="-30dp"/>

</LinearLayout>

导致结果如下图所示:

http://i.stack.imgur.com/jEdxx.png


1
我非常怀疑你能做到那一点。在处理时,我遇到的所有选项都是针对文本小于视图的情况。这是我遇到的第一个要求相反的问题。 我之前使用过一个选项,它允许您指定文本是否允许比视图更宽。

有趣的问题。如果您能添加一些代码就更好了。


谢谢你的回答,实际上这对我的项目并没有什么用处,但我只是好奇... - user2194312

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接