Android-第二页:Android UI 的开发

本文介绍了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弃用,因此应用来发一般情况下不推荐使用绝对布局。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

Android UI开发专题 Android UI开发专题(一) 之界面设计 近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关OpenGL ES相关的可能将放到以后再透露。本次主要涉及以下四个包的相关内容:  android.content.res 资源类   android.graphics 底层图形类   android.view 显示类   android.widget 控件类   一、android.content.res.Resources   对于Android平台的资源类android.content.res.Resources可能很多网友比较陌生,一起来看看SDK上是怎么介绍的吧,Contains classes for accessing application resources, such as raw asset files, colors, drawables, media or other other files in the package, plus important device configuration details (orientation, input types, etc.) that affect how the application may behave.平时用到的二进制源文件raw、颜色colors、图形drawables和多媒体文件media的相关资源均通过该类来管理。   int getColor(int id) 对应res/values/colors.xml   Drawable getDrawable(int id) 对应res/drawable/   XmlResourceParser getLayout(int id) 对应res/layout/   String getString(int id) 和CharSequence getText(int id) 对应res/values/strings.xml   InputStream openRawResource(int id) 对应res/raw/   void parseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle) 对应res/xml/   String[] getStringArray(int id) res/values/arrays.xml   float getDimension(int id) res/values/dimens.xml
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值