TextView垂直和水平居中
我试图垂直和水平地居中TextView的上下文。 请看下面的截图。

  表示1-5和100+ Points的TextViews是我想要垂直和水平居中的那些100+ Points 。  正如你所看到的,它们是水平居中但不垂直。  以下是我正在使用的代码。 
    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <TextView 
                android:id="@+id/l1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" /> 
            <TextView 
                android:id="@+id/g1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" /> 
            <TextView 
                android:id="@+id/c1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" />
        </LinearLayout>
再次,这个问题是如何集中TextViews的内容,而不是如何将TextViews集中在LinearLayout中。 那么,我如何在TextViews中以两种方式对内容进行集中?
这是你的问题:
android:layout_height="wrap_content"
  你的TextView被设置为wrap_content ,这意味着没有空间可以垂直对齐。  要解决这个问题,请设置一个固定的行高。  在我的例子中,我使用了50dp;  然而,它可以设置为任何你想要的。 
  PS - android:gravity="center"处理center_vertical和center_horizontal 。 
抛出此代码并检出它!
<LinearLayout
    android:id="@+id/linearlayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"   
        android:layout_height="50dp"
        android:orientation="horizontal" >
        <TextView 
            android:id="@+id/l1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" /> 
        <TextView 
            android:id="@+id/g1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" /> 
        <TextView 
            android:id="@+id/c1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" />
    </LinearLayout>
它看起来像安卓正在集中文本的基线,而不是文本本身。
这是一个很好的博客文章,讨论这个烦人的问题:http://www.doubleencore.com/2013/10/shifty-baseline-alignment/
  总之,如果可能的话,线性布局将试图通过文本基线来对齐子视图。  有一个xml标志可禁用此行为: android:baselineAligned ,您可以尝试将其设置为false 。 
你可以查看这篇文章
Android视图组使用了一个LayoutParams的概念,这与Swing布局管理器使用的布局约束类似。 LayoutParams配置Android View组展示其子视图的方式。 在Android中,可以使用Java代码或使用XML文件以编程方式构建GUI。 XML元素的结构与您可能以编程方式构建的View对象的结构相似。 XML属性用于配置布局参数。
您可以将重力配置到布局中。
链接地址: http://www.djcxy.com/p/56299.html