今天看教程上面说 layout_width为fill_parent时,layout_weight的值表示该控件或布局的加载顺序,值越大加载顺序靠后。下面是例子
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#000000"
android:text="TextView"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button"
/>
</LinearLayout>
TextView宽度填充父窗体所以button显示不出来。
如果在TextView中设置:layout_weight="100",模拟器效果:
之后在网上看了个帖子,说的明白些
在layout_width设置为fill_parent的時候,layout_weight所代表的是你的控件要优先尽可能的大,但这个大是有限度的,即fill_parent.
在layout_width设置为wrap_content的時候,layout_weight所代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
下面做个简单的登陆页面:
效果图:
框架图:
登录窗体设计图:
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@android:drawable/title_bar"
android:gravity="center"
android:text="登陆"
android:textColor="#000000"
android:textSize="20dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:gravity="center"
android:orientation="vertical"
android:padding="32dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/title_bar"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#000000"
android:text="欢迎" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="center"
android:textColor="#000000"
android:text="账号:" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="用户名/手机号/邮箱" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="center"
android:textColor="#000000"
android:text="密码:" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="密码" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|left" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|right" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#0080FF"
android:text="忘记密码" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="登陆" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="快速注册" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@android:color/background_dark"
android:gravity="center"
android:text="版权所有:Copyright 2013 xxxx"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>