2013-05-12 66 views
12

根據另一個已存在的視圖,是否可以使用RelativeLayout居中水平或垂直對齊XML中的視圖。RelativeLayout:align相對於其他視圖居中水平或垂直

例如:可以說有這樣的事情:

enter image description here

第二個文本視圖應顯示爲中心的第一個文本視圖下方:

<?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" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="72dp" 
     android:text="dynamic text" /> 

    <TextView 
     android:id="@+id/second" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textView" 
     android:layout_marginLeft="43dp" <!-- Is there a rule to center it? --> 
     android:text="centered below text 1" /> 

</RelativeLayout> 

是有可能實現類似於XML中的東西?有沒有規則,我錯過了?我不想計算位置編程

回答

12

以下內容使用一個單獨的父佈局的意見,並在主佈局添加它(可以包含其他的事情,如果你有)

<?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" > 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     android:layout_marginLeft = "30dp"> 
     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="dynamic text" /> 

     <TextView 
      android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="centered below text 1" /> 
    </LinearLayout> 
... 
... 
other veiws go here 
... 

</RelativeLayout> 
+14

不,我不想將它居於父母 – sockeqwe 2013-05-12 19:00:42

+0

你需要它 – Noundla 2013-05-12 19:01:18

+0

我修改了答案。檢查一次。 – Noundla 2013-05-12 19:04:59

4

使用適合你

android:layout_centerHorizontal="true" 
    android:layout_centerInParent="true"  
    android:layout_centerVertical="true"  
+4

不,我不想居中父...我要居中低於或高於一定的另一種觀點。例如:另一個視圖有一個marginLeft =「72dp」 – sockeqwe 2013-05-12 18:47:56

+0

是的只是使用centerhoarizontal = true並將視圖置於另一個之上或之下 – stinepike 2013-05-12 18:49:03

+0

嗯,也許我弄錯了,但是應用這個將父母中的第二個文本視圖居中。但我希望它被集中在其他視圖下面,所以根據第一個視圖居中相對是我想要的......我想這可以通過將這兩個視圖包裝在另一個RelativeLayout – sockeqwe 2013-05-12 19:00:08

0

不要這個 - >

<?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" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="dynamic text" /> 

    <TextView 
     android:id="@+id/second" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView" 
     android:layout_centerHorizontal="true" <!-- Is there a rule to center it? --> 
     android:text="centered below text 1" /> 

</RelativeLayout> 
0

我找到了這樣lution: 包裹內容到另一個RelativeLayout的,然後你可以把這個LayoutWrapper無論你想:

<?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" > 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="58dp" 
     android:layout_marginTop="58dp" > 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerInParent="true" 
      android:text="dynamic text" /> 

     <TextView 
      android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView" 
      android:layout_centerInParent="true" 
      android:text="centered below text 1" /> 
    </RelativeLayout> 

</RelativeLayout> 
20

我比接受一個更好的解決方案。沒有額外的睡覺!您可以通過在較小的視圖上組合兩個屬性來完成此操作。如果您正在水平居中,則可以將align_start & align_end同時用於較大視圖。確保文本重心居中順便說一句。「機器人:重力=」中心」垂直對齊同時使用ALIGN_TOP & ALIGN_BOTTOM下面是佈局的修改實施

<?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:paddingLeft="43dp" > 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignStart="@+id/second" 
    android:layout_alignEnd="@+id/second" 
    android:gravity="center" 
    android:text="dynamic text" /> 

<TextView 
    android:id="@+id/second" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textView" 
    android:gravity="center" 
    android:text="centered below text 1" /> 

</RelativeLayout> 

無需像接受不必要的嵌套。的回答。

+0

是的,但只有當你知道textview的寬度來調整邊距時纔有效。 – sockeqwe 2014-10-15 20:13:56

+0

沒有必要保證金....保證金是你最初的。會發生什麼情況是,較小的文本的寬度等於較大的文本,並且通過具有重心,視圖將相對於較大的文本視圖居中。要驗證這一點,請添加第二個文本視圖的邊距或文本內容,並觀察第一個移動。 – Jessicardo 2014-10-15 20:50:13

+0

我也刪除了顯式邊距,添加了填充到佈局容器,並更新了佈局。 – Jessicardo 2014-10-15 20:55:49

0

正確的解決方案是使用ConstraintLayout

我試圖對準一個TextView水平低於在RelativeLayout的一個按鈕,但它是不可能的,因爲ALIGN_BOTTOM和layout_below沒有發揮得很好。最後我PI啓動了constraintLayout並嘗試了相同的邏輯,並且它像魅力一樣工作。這裏是下面的代碼。

<android.support.constraint.ConstraintLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clipChildren="false" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<Button 
    android:id="@+id/episode_recording_header_stop" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@mipmap/ic_launcher" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.399" /> 


<TextView 
    android:id="@+id/button_selected_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp" 
    android:textAlignment="center" 
    android:textColor="@android:color/black" 
    android:textSize="12sp" 
    android:text="text which is exactly center aligned w.r.t the Button above" 
    app:layout_constraintEnd_toEndOf="@+id/episode_recording_header_stop" 
    app:layout_constraintStart_toStartOf="@+id/episode_recording_header_stop" 
    app:layout_constraintTop_toBottomOf="@+id/episode_recording_header_stop" 
    /> 

</android.support.constraint.ConstraintLayout> 

最終的輸出連接下面

enter image description here

+0

正如你所看到的對齊是w.r.t錨點視圖(按鈕)而不是父視圖或任何其他視圖。 – 2018-01-04 06:58:52

+0

是的,只是使用ConstraintLayout,因爲它允許做類似的東西(RelativeLayout不)... – sockeqwe 2018-01-04 07:19:23

+0

@sockeqwe接受的答案不回答問題的權利?另外,我花了一些時間才意識到在另一個視圖中,視圖的水平對齊在RelativeLayout中是不可能的。正確的答案是現在使用ConstraintLayout。 – 2018-01-04 07:40:17