android UI设计:用布局中ayout_weight设置及简单登陆页面

本文详细解析了Android中LinearLayout布局的layout_weight属性作用机制,并通过一个登录界面实例展示了如何使用layout_weight来实现灵活的UI布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天看教程上面说 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>


 

 参考贴:http://www.oschina.net/question/166763_54906

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值