Android开发之布局方式

在Android主要的布局方式线性布局(LinearLayout)、相对布局(RelativeLayout)、帧布局(FrameLayout)、表格布局(TableLayout)。前两者是常用的,所以今天就着重的讨论一下LinearLayout线性布局。

    线性布局可以沿水平方向或者垂直方向上来排列你的控件。 比如你的控件是垂直排列的,那么你就可以通过指定控件水平的居中方式(这一点可能说起来比较抽象,下方会通过实例来进行介绍)。接下来将通过简单的实例来介绍一下LinearLayout。我们在使用LinearLayout时可以通过设置一些属性来进行对内容的调整,这个就跟之前学习的CSS样式差不多。下面是一些常用的样式:

  1. android:id属性代表着此控件的Id,也就是此控件的唯一标示,在java代码中我们可以通过findViewById()方法来通过Id获取控件。
  2. android:layout_width属性代表着控件的宽度,当属性的值是match_parent, 表示该控件的宽度与父视图的宽度相同,而当属性的值是wrap_content,表示控件的高度根据内容的高度进行改变。
  3. android:layout_height属性代表着控件的高度,属性值与上一个描述的一样
  4. android:layout_padding、layout_margin属性可设置他的内外边距
  5. android:orientation设置此布局里的控件排布垂直方向
  6. android:visibility设置此标签内容显示与隐藏属性,最常用的是gone值,然后通过代码可进行一个隐藏与显示的功能。

下面我们通过一个最简单的账户登录例子来简单的看了解一下,效果图:

   

下面来看一下代码实现,即 .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:orientation="vertical">

    <!—当我们想要在手机中显示的位置位于中心位置时可使用一个标签占位-->

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="0dp"

        android:layout_weight="1" />

    <!--表单布局-->

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:padding="10dp"

        android:paddingLeft="16dp"

        android:orientation="vertical">

        <!--账号-->

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <!--TextView文本 html的p标签-->

            <TextView

                android:layout_width="70dp"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="账     号:"/>

            <EditText

                android:id="@+id/et_login_phone"

                android:layout_width="match_parent"

                android:inputType="phone"

                android:maxLength="11"

                android:hint="请输入账号"

                android:layout_height="wrap_content"/>

        </LinearLayout>

        <!--密码-->

        <LinearLayout

            android:id="@+id/ll_login_password"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal"

            >

            <!--TextView文本 html的p标签-->

            <TextView

                android:layout_width="70dp"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="密      码:"/>

            <EditText

                android:id="@+id/et_login_password"

                android:layout_width="match_parent"

                android:hint="请输入密码"

                android:inputType="textPassword"

                android:layout_height="wrap_content"/>

        </LinearLayout>

        <!--登录按钮-->

        <Button

            android:id="@+id/btn_login_submit"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:background="@color/colorPrimary"

            android:textSize="22sp"

            android:text="登 录"/>

    </LinearLayout>

    <!--占位-->

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="0dp"

        android:layout_weight="1"/>

</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值