登陆界面 LinearLayout的属性的用法

来源:互联网 发布:百世汇通软件 编辑:程序博客网 时间:2024/06/07 14:59

通过设计登录界面的实例来了解LinearLayout一些属性的用法


要点:

android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,gravity如果需要设置多个属性值,需要使用“|”进行组合

android:gravity 与 android:layout_gravity的区别
android:gravity是指定本元素的子元素相对它的对齐方式。
android:layout_gravity是指定本元素相对它的父元素的对齐方式。

<?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:orientation="vertical" >       <LinearLayout        android:layout_width="match_parent"        android:layout_height="400dp"        android:orientation="vertical" >        <!-- 标题 -->        <!-- android:gravity="center"  因为有子控件,所以作用是设置子控件在本控件中的位置 -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="3"            android:gravity="center"             android:orientation="vertical" ><!--  android:gravity="center"  因为没有子控件,所以作用是设置文字在TextView中的位置 -->            <TextView                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:gravity="center"                 android:text="淘宝客户端"                android:textSize="25sp"/>          </LinearLayout>        <!-- 内容 -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:orientation="horizontal"            android:padding="5dp" ><!-- layout_weight="3"使TextView占宽度的1/4  layout_weight="1"使EditText占比重的3/4-->            <TextView                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="3"                android:gravity="center"                android:text="账 号"                android:textSize="18sp" />            <EditText                android:id="@+id/zhanghao"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1"                android:hint="输入账号名"                android:textSize="18sp" />        </LinearLayout><!-- android:padding="5dp" 使子控件距离边缘5dp -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:orientation="horizontal"            android:padding="5dp" >            <TextView                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="3"                android:gravity="center"                android:text="密 码"                android:textSize="18sp" />            <EditText                android:id="@+id/zhanghao"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1"                android:hint="输入密码"                android:textSize="18sp" />        </LinearLayout>        <!-- 按钮 -->                <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="3"            android:gravity="center"            android:orientation="horizontal" >            <Button                android:id="@+id/login"                android:layout_width="120dp"                android:layout_height="wrap_content"                android:text="登录" />            <Button                android:id="@+id/login"                android:layout_width="120dp"                android:layout_height="wrap_content"                android:text="注册" />       </LinearLayout>    </LinearLayout></LinearLayout>



0 0
原创粉丝点击