2014-09-23 59 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

相关问题