2014-06-22 55 views
0

我想将水平方向上的TextViewRelativeLayout对齐。 尝试所有的选项,你可以看到下面,但没有一个似乎有所作为。仅在水平方向上对齐RelativeLayout中的TextView

唯一不同的是android:layout_centerInParent="true"但是这个问题是它也垂直居中,但我只是想横向。

请建议。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/relativeLayoutFragment" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" > 

     <TextView 
      android:id="@+id/userLocation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="@dimen/marginOne" 
      android:background="#80000000" 
      android:gravity="center" 
      android:padding="@dimen/marginOne" 
      android:text="@string/findingLocation" 
      android:textColor="#FFFFFF" 
      android:textSize="@dimen/FontTwo" /> 

    </RelativeLayout> 

回答

1

你需要在你相对布局添加重力android:gravity="center_horizontal"

样本:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayoutFragment" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal"> 

    <TextView 
     android:id="@+id/userLocation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/marginOne" 
     android:background="#80000000" 
     android:gravity="center" 
     android:padding="1dp" 
     android:text="I am here" 
     android:textColor="#FFFFFF" 
     android:textSize="40sp" /> 

</RelativeLayout> 

结果:

enter image description here

+0

没有变化,都一样。 – Maven

+0

@Maven你确定,它工作正常..看上面,看看编辑 –