2014-09-23 35 views
0

我試圖在一個相對佈局的中心微調中心。 我嘗試如下:如何在Android中使用RelativeLayout對齊微調視圖?

<LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/typesSpinner"/> 

    </LinearLayout> 

這很適合我在其他情況下,但不與微調。 爲什麼?

+1

你提到試圖將它居中在一個RelativeLayout,但你的例子是使用LinearLayout。 – 2014-09-23 21:19:39

+0

我實際上的意思是使用線性佈局,因爲我習慣了它。我不知道它的相對佈局簡單 – buddy123 2014-09-23 21:30:42

回答

3

假設您確實需要RelativeLayout而不是LinearLayout作爲包裝。下面應該工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Spinner 
     android:id="@+id/section_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true"/> 

</RelativeLayout> 
+1

你也可以使用'android:layout_centerInParent =「true」'。 – 2014-09-23 21:25:29

+0

@XaverKapeller是的,一行肯定比2更好,我會更新我的答案 – Naveed 2014-09-23 21:27:03

+0

我其實打算使用Linear,但是由於它在Relative中的工作非常簡單,所以我不介意。謝謝 – buddy123 2014-09-23 21:31:35

相關問題