Android 安卓:什么时候使用版面空白,什么时候使用权重和?

Android 安卓:什么时候使用版面空白,什么时候使用权重和?,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我正在尝试在我的Android应用程序上实现以下布局: 正如你所看到的,我想要一个占70%宽度的中央登录框。我认为基本上有两种方法可以实现这一目标: 1。为登录框创建线性或相对布局,并在左侧和右侧添加layoutMargin 2.使用weightSum=“1”创建包装线布局,然后使用layout\u width=“0dp”和layout\u weight=“0.7”将相对线布局放在其中 使用第一种方法,XML将如下所示: <LinearLayout xmlns:android="http

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

正如你所看到的,我想要一个占70%宽度的中央登录框。我认为基本上有两种方法可以实现这一目标:

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

2.使用weightSum=“1”创建包装线布局,然后使用layout\u width=“0dp”和layout\u weight=“0.7”将相对线布局放在其中

使用第一种方法,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>

第二种方法只是在当前方法的顶部有一个wrapper LinearLayout,weightSum=“1”,而内部布局将获得一个layout\u weight=“0.7”属性


你认为哪一个是最好的方法?提前感谢。

有一个3d替代方案,在LinearLayout/RelativeLayout中创建登录框,然后将其水平居中对齐。

一个相当简单的方法是在相对布局中使用线性布局。然后使用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%,您可以通过编程实现。

但是如果我不使用边距,登录框将占据整个宽度,这不是我想要的。如果我在登录框上使用width=“wrap\u content”,我需要为编辑文本设置一个固定的宽度。从我的角度来看,在这种情况下不需要边距,只需让登录框
布局\u width=“wrap\u content”
(以及布局高度),它会占用尽可能多的空间。但是,你怎么才能让EditText在登录框中具有相同的宽度呢?android:gravity=“center”的线性布局可以使EditText居中,但如果我在其上使用layout\u width=“wrap\u content”,它们的大小仍然是可变的。这几乎就是方法n。1我在我的问题中提到过,不是吗?我会用一个重要的提示说是的。您为父布局建议了相对或线性布局。我选择相对,因为这允许我使用layout\u centerInParent参数。在这一点上,将边距设置为你认为合适的边距是很简单的。即使是线性的,这也会起作用。你只需要安卓:gravity=“center”就可以了。我认为这是正确的。现在,我想,你可能想考虑切换我上面所用的框架布局,而不是相对布局。它应该实现相同的外观,但加工成本更低,因此性能更好。不过,在应用程序的上下文中,这可能并不重要。