2015-06-10 83 views
-1

我的目标是将背景白色的形式放在屏幕的中心。只是跟着其他一些答案,但我无法实现我的目标。Android布局:位置布局的孩子在屏幕中心位置?

只有当我移除背景为白色时,布局才会居中。但是,我有背景图片,所以我需要(并且我想学习!)插入白色背景以帮助用户。

非常感谢您

enter image description here

这一点,在activity_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:background="@drawable/background" 
    android:scaleType="centerCrop" 
    tools:context=".LoginActivity"> 

     <LinearLayout 
      android:background="#FFFFFF" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:gravity="center" > 


      <EditText android:id="@+id/username" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/username" 
       /> 

      <EditText android:id="@+id/password" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/login" 
       android:onClick="performLogin" /> 

     </LinearLayout> 

    </RelativeLayout> 

这是styles.xml

<resources> 



    <!-- Base application theme. --> 
    <style name="MyAppTheme" parent="Theme.AppCompat.Light"> 

    </style> 

</resources> 

谢谢您的帮助!

回答

2

将标签android:layout_centerVertical="true"添加到您的LinearLayout以垂直居中。