如何在TextView的垂直对齐文本 [英] How to vertically align text within TextView

查看:148
本文介绍了如何在TextView的垂直对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一些文字一个TextView内垂直排列,但由于某些原因,它只是坚持这样的TextView的顶部边缘。

相信我,我已经读到这里的几个问题就相同或相似,问题的堆栈溢出,并试图应用提供的解决方案,但似乎没有做出改变。

举例来说,我认为会解决这个问题马上就是添加属性安卓重力=center_vertical |左,但这并没有帮助

该用户的<一个href=\"http://stackoverflow.com/questions/18724144/can%C2%B4t-center-textview%C2%B4s-text-vertically\">question描述了几乎整整我的问题,只是他的TextView是一个RelativeLayout的这又是一个滚动型,这是我没有(也不需要或希望)使用内内。而他所提供的图像也完全适用于什么,我想描述并的实现

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:weightSum =2&GT;    &LT; ImageView的
        机器人:ID =@ + ID / account_server_icon
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/ account_server_icon
        机器人:paddingLeft =16DP
        机器人:重力=左/&GT;    &LT;的TextView
        机器人:方向=垂直
        机器人:ID =@ + ID / tv_account
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingLeft =16DP
        机器人:重力=左| center_vertical/&GT;&LT; / LinearLayout中&GT;


解决方案

的问题是,你的的TextView 的高度设置为 WRAP_CONTENT 。这意味着,文字的容器的大小将是相同文本的高度,所以真的是无处视图中居中的内容(这是有效的已垂直居中)。

如果在的LinearLayout 您发布的只有的包含的ImageView 的TextView ,那么你可以简单的改变高度 match_parent ,它应该工作正常。

如果在的LinearLayout 包含其他查看 S,你可能要考虑使用 RelativeLayout的并设置的TextView 与图像的顶部和底部对齐。

I am trying to get some text vertically aligned within a TextView, but for some reason it just sticks to the top edge of such TextView.

Trust me, I've read several questions here on Stack Overflow regarding the same, or similar, questions and tried applying the provided solutions, but nothing seems to make a change.

For instance, what I thought would solve the issue right away was to add the attribute android:gravity="center_vertical|left", but that didn't help either.

This user's question describes almost exactly my issue, except that his TextView is inside a RelativeLayout which in turn is inside a ScrollView, which I don't (and don't need or want to) use. And the images he provided are also perfectly applicable to what I'm trying to describe and achieve.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2" >

    <ImageView
        android:id="@+id/account_server_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/account_server_icon"
        android:paddingLeft="16dp"
        android:gravity="left" />    

    <TextView
        android:orientation="vertical"
        android:id="@+id/tv_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:gravity="left|center_vertical" />

</LinearLayout>

解决方案

The problem is that your TextView's height is set to wrap_content. This means that the size of the text's container will be the same as the height of the text, thus there really is nowhere within the view to center the content (it is effectively already centered vertically).

If the LinearLayout that you posted only contains the ImageView and the TextView, then you can simple change the height to match_parent and it should work fine.

If the LinearLayout contains other Views, you might want to consider using a RelativeLayout and setting the TextView to align with both the top and bottom of the image.

这篇关于如何在TextView的垂直对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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