Android:什么时候使用layout_margin,什么时候使用weightSum? [英] Android: When to use layout_margin, when to use weightSum?
问题描述
我正在尝试在Android应用上实现以下布局:
I am trying to achieve the following layout on my Android app:
如您所见,我希望中央登录框占用大约70%的宽度.我相信基本上有两种方法可以实现这一目标:
As you can see I want a central login box taking about 70% of the width. I believe there are basically two approaches to achieve this:
1..为登录框创建线性或相对布局,并在左右两侧添加layoutMargin.
1. Create a Linear or Relative layout for the login box, and add layoutMargin both on the left and right sides.
2..创建一个带有weightSum ="1"的包装器LinearLayout,然后将其中的RelativeLayout放在layout_width ="0dp"和layout_weight ="0.7"的内部.
2. Create a wrapper LinearLayout with weightSum="1", and then place the RelativeLayout inside it with layout_width="0dp" and layout_weight="0.7".
使用第一种方法,XML如下所示:
Using the first approach the XML would look like this:
<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>
第二种方法是在当前方法的顶部添加一个包装LinearLine,重量为weightsum ="1",内部布局将获得layout_weight ="0.7"属性.
The second approach would just have a wrapper LinearLayout on top of the current one, with weightSum="1", and the internal layout would get a layout_weight="0.7" attribute.
您认为哪种方法最好?预先感谢.
Which one do you think is the best approach? Thanks in advance.
推荐答案
一种相当简单的方法是在相对布局内使用线性布局.然后使用layout_centerInParent并将其设置为"true".完成此操作后,您可以将余量设置为您想要的余量(我使用了30 dp).这是一个示例:
A rather simple way would be use a linear layout inside a relative layout. Then use layout_centerInParent and set it to "true". Once that is done you can set your margin to what ever you would like (I used 30 dp). Here is an example:
<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%,则可以通过编程方式进行.
Of course, if you wanted the width of the log in box to be exactly 70 percent of the width of the screen, you could do it programmatically.
这篇关于Android:什么时候使用layout_margin,什么时候使用weightSum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!