2016-04-04 43 views
0

如何在中心對齊的相對佈局中放置兩個文本視圖Marked within red rectangle。我如何設計如圖所示? 這裏的文本和數字都是textviews。如何以相對佈局爲中心對齊兩個文本視圖

enter image description here

我曾嘗試以下,但我不能讓中心對齊以相對佈局。

 <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="50dp"> 

      <LinearLayout 
       android:id="@+id/location_party_size_lbl_layer" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_centerInParent="true" 
       android:orientation="horizontal"> 

       <TextView 
        android:id="@+id/location_party_size_lbl" 
        android:layout_width="170dp" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp" 
        android:gravity="center_horizontal" 
        android:maxLines="2" 
        android:text="@string/loc_estimate" 
        android:textColor="@android:color/white" 
        android:textSize="15sp" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/location_party_size_lbl_layer" 
       android:orientation="horizontal" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:weightSum="1.0"> 

       <TextView 
        android:id="@+id/location_party_size" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.48" 
        android:background="@drawable/rounded_bg" 
        android:gravity="center" 
        android:textColor="@android:color/white" /> 

       <TextView 
        android:id="@+id/qnowscreen_dummy_tv" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.52" 
        android:visibility="invisible" /> 
      </LinearLayout> 
     </RelativeLayout> 
+0

也許你可以使用一個linearlayout的方向水平和權重的子女? – Bram

+0

你可以從你的xml標籤中指出,哪兩個textiviews要中心對齊,如果你想讓它們垂直和水平居中對齊,就好像你有兩個,它們會重疊?你想如何將它們放在一起?從圖像中不清楚。 –

+0

@ Ms YvetteǝʇʇǝʌʎsW,location_party_size_lbl用於顯示文本「估計....」和用於顯示「2」的location_party_size –

回答

4

你可以通過創建一個錨點視圖和相對對準TextView s到錨。

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

    <View 
     android:id="@+id/anchor" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerInParent="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/anchor" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/anchor" 
     android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 
+0

它垂直居中對齊,不水平居中對齊。 –

+0

似乎我誤解了你的問題。不幸的是,你想要的是不可能用'RelativeLayout'實現的。 – Michael

相關問題