你怎么中心的布局一个TextView? [英] How do You Center a TextView in Layout?

查看:170
本文介绍了你怎么中心的布局一个TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的布局,其中一部分设有集中于一个标签的值,以+和 - 按钮上的值的任一侧。我想要的值的按钮之间居中,无论是1或99。它看起来很好,当它像99的2位数字,但是当它是一个数字的号码是​​左对齐。我该如何正确中心的价值呢?

下面是我的布局,这是否该部分:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_above =@ ID / runway_label
            机器人:layout_centerHorizo​​ntal =真
            机器人:方向=横向>
            < ImageView的机器人:ID =@ + ID / dec_runway_button机器人:SRC =@可绘制/ minus_button
                机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =center_vertical/>
            < TextView的机器人:ID =@ + ID / runway_value
                机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                机器人:文字颜色=#FFFFFF
                机器人:TEXTSTYLE =黑体的Andr​​oid版本:TEXTSIZE =40.0sp
                机器人:=了minWidth50SP
                机器人:layout_centerInParent =真
                机器人:layout_gravity =中心
                机器人:则shadowColor =#333333机器人:shadowDx =2.0
                机器人:shadowDy =2.0的Andr​​oid版本:shadowRadius =3.0/>
            < ImageView的机器人:ID =@ + ID / inc_runway_button机器人:SRC =@可绘制/ plus_button
                机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =center_vertical/>
        < / LinearLayout中>
 

解决方案

这听起来像你想中心内的的TextView ,而不是的TextView 的布局。

您已经很明确地设置了minWidth 的TextView 的,这是很好的。您是否尝试过使用安卓重力=center_horizo​​ntal的TextView ? NOT layout_gravity 这是的TextView 的母公司内的严重性,在这种情况下的LinearLayout

I have a complex layout, part of which features a value centered over a label, with + and - buttons on either side of the value. I want the value to center between the buttons, whether it is "1" or "99". It looks fine when it's a 2-digit number like "99", but when it's a single digit the number is left-justified. How do I properly center that value?

Here's the portion of my layout that does this:

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/runway_label"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">
            <ImageView android:id="@+id/dec_runway_button" android:src="@drawable/minus_button"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"/>
            <TextView android:id="@+id/runway_value"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor="#FFFFFF"
                android:textStyle="bold" android:textSize="40.0sp"
                android:minWidth="50sp"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:shadowColor="#333333" android:shadowDx="2.0"
                android:shadowDy="2.0" android:shadowRadius="3.0" />
            <ImageView android:id="@+id/inc_runway_button" android:src="@drawable/plus_button"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"/>
        </LinearLayout>

解决方案

It sounds like you want to center the text within the TextView, not the TextView in the Layout.

You're already explicitly setting the minWidth of the TextView, which is good. Have you tried using android:gravity="center_horizontal" for the TextView? NOT layout_gravity which is the gravity of the TextView within the parent, in this case LinearLayout.

这篇关于你怎么中心的布局一个TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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