Android:何时使用layout_margin,何时使用weightSum?

时间:2012-10-12 17:37:56

标签: android android-layout android-linearlayout

我正在尝试在我的Android应用上实现以下布局:

enter image description here

正如您所看到的,我希望中央登录框占据宽度的70%左右。我相信基本上有两种方法可以做到这一点:

1。为登录框创建线性或相对布局,并在左侧和右侧添加layoutMargin。

2. :使用weightSum =“1”创建包装器LinearLayout,然后使用layout_width =“0dp”和layout_weight =“0.7”将RelativeLayout放入其中。

使用第一种方法,XML将如下所示:

<LinearLayout 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:orientation="vertical">

    <ImageView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:src="@drawable/logo"
        android:contentDescription="@string/logo"  
         />

    <LinearLayout
        android:layout_width="match_parent"         
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#999999"
        android:gravity="center"
        android:layout_margin="30dp"
         >
        <EditText
            android:layout_width="fill_parent"           
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:hint="@string/email" />
        <EditText
            android:layout_width="fill_parent"            
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:hint="@string/password" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  
            android:text="@string/login"/>


    </LinearLayout>     


</LinearLayout>

第二种方法只是在当前的一个上面有一个包装器LinearLayout,weightSum =“1”,内部布局将获得layout_weight =“0.7”属性。

您认为哪一种方法最好?提前谢谢。

2 个答案:

答案 0 :(得分:2)

一种相当简单的方法是在相对布局中使用线性布局。然后使用layout_centerInParent并将其设置为“true”。一旦完成,你可以设置你想要的余量(我使用30 dp)。这是一个例子:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_margin="30dp"
    android:orientation="vertical"
    android:weightSum="50" >

    <EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:inputType="textEmailAddress" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/editText1"
    android:layout_centerHorizontal="true"
    android:inputType="numberPassword" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/editText2"
    android:layout_centerHorizontal="true"
    android:text="Button" />
</LinearLayout>

</RelativeLayout>

当然,如果您希望登录框的宽度恰好是屏幕宽度的70%,则可以通过编程方式进行。

答案 1 :(得分:0)

有一个3d选择,在LinearLayout / RelativeLayout中创建登录框,然后将其水平对齐。