2014-06-23 4 views
4
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="78dp" 
     android:layout_marginTop="199dp" 
     android:text="test" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:text="I want to align Center" /> 

</RelativeLayout> 

여기 내 XML 코드입니다.RelativeLayout 방법 센터 정렬 android

TextView2에서 layout_alignCenter = "@ + id/textView1"을 사용하고 싶습니다.

하지만 alignCenter가 없습니다.

layout_centerVertical 또는 centerinParent도 쓸모가 없다고 생각합니다.

답변

2

당신은 LinearLayout의 경우 a RelativeLayout 포장 및 Space 요소를 사용할 수 있습니다

<LinearLayout layout_width="0dp" layout_height="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
    <Space 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="78dp" 
      android:layout_marginTop="199dp" 
      android:text="test" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView1" 
      android:text="I want to align Center" /> 

    </RelativeLayout> 
    <Space 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
</LinearLayout> 

Space 요소는 레이아웃 가중치에 따라, 상대적으로 모든 Space 요소 사이의 빈 공간을 배포합니다.

9

텍스트를 표시하려고 할 때 정확한 화면 중앙을 확인하고 .... 수정 된 코드를 사용하십시오.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_centerInParent="true" 
    android:text="test" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:text="I want to align Center" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_centerInParent="true"/> 

</RelativeLayout> 

이것은 분명히 유용 할 것입니다. 다른 요구 사항이 있으면 자세히 설명하십시오.

+1

예 완벽하게 작동합니다. –

+0

답변을 수락하십시오! – John