简体   繁体   English

TextView和RelativeLayout中的双中心文本问题

[英]Double center text issue in TextView and RelativeLayout

I've a problem centering a text in an already centered TextView: 我在将文本居中放置在TextView中时遇到问题:

That's the layout: 那是布局:

<RelativeLayout>... 
<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/w0"
    android:layout_margin="10dip"
    android:background="@color/green1"
    />

<TextView
    android:id="@+id/temperature"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/image"
    android:layout_alignTop="@id/image"
    android:layout_toRightOf="@id/image"
    android:text="15º"
    android:textSize="60dip"
    style="bold"
    android:textColor="@color/blue"
    android:gravity="center"
    android:layout_marginLeft="7dip"
    android:background="@color/green1"
    />

<TextView
    android:id="@+id/min_temperature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/temperature"
    android:layout_toRightOf="@id/temperature"
    android:text="M 21º"
    android:textSize="20dip"
    style="bold"
    android:textColor="@color/blue"
    android:layout_marginLeft="5dip"
    android:background="@color/orange1"
    />

<TextView
    android:id="@+id/max_temperature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/temperature"
    android:layout_toRightOf="@id/temperature"
    android:text="m 10º"
    android:textSize="20dip"
    style="bold"
    android:textColor="@color/blue"
    android:layout_marginLeft="5dip"
    android:background="@color/orange2"
    />

And that's the displayed layout image: 这就是显示的布局图像:

布局图

What I don't understand is why the 15º isn't centered in the TextView. 我不明白的是为什么15º不在TextView中居中。 The problem is due the TextView's forced center using alignTop and alignBottom but I can't see why it doesn't works. 问题是由于TextView的使用AlignTop和alignBottom的强制居中,但是我看不到为什么它不起作用。 I know I can solve this using some nesting layouts but I prefer to get a solution using only the RelativeLayout or if it isn't possible understand the reason. 我知道我可以使用一些嵌套布局来解决此问题,但是我更喜欢仅使用RelativeLayout获得解决方案,或者如果无法理解原因,则可以。

Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now. 还有一种解决方案,可以像现在一样,将最大和最小TextView与15º文本(不是TextView)的顶部和底部对齐。

Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now. 还有一种解决方案,可以像现在一样,将最大和最小TextView与15º文本(不是TextView)的顶部和底部对齐。

  • I don't think so. 我不这么认为。 You can only make things relative to the actual box that the View represents. 您只能使事物相对于View所代表的实际盒子。 And in the case of TextView the box is larger than the Text. 对于TextView,此框大于Text。 Any relations that you do make are going to use the position of the Box no matter where the text is at or what it looks like. 无论文本位于何处或外观如何,您所做的任何关系都将使用Box的位置。

Try using 尝试使用

android:layout_centerVertical="true" android:layout_centerVertical =“ true”

on your temperature TextView in addition to the gravity you have now. 在您的温度TextView上,以及您现在拥有的重力上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM