Android-第二页:Android UI 的开发

UI界面在Android应用中是非常重要的,它是由View和ViewGroup对象构建的,VierGroup是View的一个扩展,可以容纳多个View,通过ViewGroup类可以创建有联系的子View组成的复合控件。
下面就来介绍UI的布局类型:

相对布局(RelativeLayout):
Android Studio中默认的即为相对布局,一般有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。

源代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button2" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button3" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Button4"
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button5" />
    />
</RelativeLayout>

线性布局(LinearLayout):
使用标签表示,
它主要有两种方式指定控件位置,水平线性布局和竖直线性布局。
android:layout_gravity:用于指定控件在布局中的对齐方式
android: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="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="bottom"
        android:text="Button1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button2" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Button3" />
</LinearLayout>

表格布局(TableLayout):
顾名思义,就是让控件以表格的形式来排列。只要是将控件放在单元格中,控件就可以整齐的排列。

源代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="2">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"/>
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="Button3"/>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="Button4"/>
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="Button5"/>
    </TableRow>
</TableLayout>

网格布局(GridLayout):
它是Android 4.0新增布局,使用时,需要把SDK的最低版本指定为Android4.0(API14)以上。如何配置SDK兼容的最低版本呢,下面就是其代码:

defaultConfig{
     minSdkVersion 15
     targetSdkVersion 25
     versionCode 1
}

只要minSdkVersion>15就是4.0以上。
网格布局实现了控件的交错显示,能够避免因布局嵌套对设备性能的影响,更利于自由布局的开发。
网格布局用一组无线细的直线将绘图区域分成行、列和单元,并指定控件的显示区域和控件在该区域的显示方式。

源代码:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:layout_gravity="center">
    <Button
        android:text="/"
        android:layout_column="3"/>
    <Button android:text="1"/>
    <Button android:text="2"/>
    <Button android:text="3"/>
    <Button android:text="*"/>
    <Button android:text="4"/>
    <Button android:text="5"/>
    <Button android:text="6"/>
    <Button android:text="-"/>
    <Button android:text="7"/>
    <Button android:text="8"/>
    <Button android:text="9"/>
    <Button
        android:text="+"
        android:layout_rowSpan="3"
        android:layout_gravity="fill"/>
    <Button
        android:text="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"/>
    <Button android:text="00"/>
    <Button
        android:text="="
        android:layout_columnSpan="3"
        android:layout_gravity="fill"/>
</GridLayout>

帧布局(FramLayout):
它是为每个加入其中的控件创建一个空白区域(成为一帧,每个控件占据一帧)。
采用帧布局设计时,只能在屏幕左上角显示一个控件,如果添加多个控件,这些控件会按照顺序在屏幕左上角重叠,且会透明显示之前的控件文本。

源代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="Button1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/button" />
    <Button
        android:text="Button2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/button2" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_gravity="right"
        android:id="@+id/imageView" />
</FrameLayout>

绝对布局(AbsoluteLayout):
绝对布局需要通过制定x、y坐标来控制每一个控件的位置,放入该布局的组件通过android:layout_x和android:layout_y两个属性指定其准确的坐标值,并显示在屏幕上。
但是,由于多分辨率,兼容太麻烦,绝对布局多用于游戏开发中,在Android 1.5后被Google弃用,因此应用来发一般情况下不推荐使用绝对布局。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值