Android学习--常见界面布局

一,界面布局的通用属性

属性名称功能描述
android:id设置布局标识
android:layout_width设置布局宽度
android:layout_height设置布局高度
android:background设置布局背景
android:layout_margin设置布局外边距
android:padding设置布局内边距

二、线性布局

线性布局内的子控件为水平或竖直排列。

1、基本语法格式
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             <!--
              属性=“属性值”...如:
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              -->
/>

match_parent代表该布局的高(宽)度与父布局一致

2、自身常用属性
属性名称功能描述
android:orientation设置布局内控件的排列方式
android:layout_weight

在布局内设置控件的权重

android:orientation:有两个可选值,分别为vertical(竖直排列)horizontal(水平排列),默认为horizontal

android:layout_weight:在控件中直接用int来表示权重。

示例:

<?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">
    <Button
            android:text="Button"
            android:layout_width="match_parent"
-->         android:layout_weight="1"
            android:layout_height="wrap_content" android:id="@+id/button6"/>
    <Button
            android:text="Button"
            android:layout_width="match_parent"
-->         android:layout_weight="1"
            android:layout_height="wrap_content" android:id="@+id/button7"/>
    <Button
            android:text="Button"
            android:layout_width="match_parent"
-->         android:layout_weight="2"
            android:layout_height="wrap_content" android:id="@+id/button8"/>
</LinearLayout>

布局结果:

3、实战演练

仿写动物连连看的游戏界面。

步骤1:将准备好的图片放置res下的drawable-hdpi文件夹下(默认情况下没有,需要自己手动创建),注意图片应用.png格式,用英文命名

步骤2:为减少代码冗余,在res/values下创建styles.xml文件,为按钮设置统一样式btnStyle

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="btnStyle">
        <item name="android:layout_width">100dp</item>
        <item name="android:layout_height">100dp</item>
        <item name="android:layout_marginRight">8dp</item>
    </style>
</resources>

步骤3:排版

在res/layout文件夹下创建layout.xml,点击右上角Design进行布局

注意这里使用的线性嵌套布局

拉好以后点击Code调整组件样式

步骤4:更改java中运行显示页面(java文件夹中的MainActivity)

layout.xml完整代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
              android:background="@drawable/background"
              android:orientation="vertical">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_weight="4"
            android:layout_height="0dp">
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_weight="5"
            android:layout_height="0dp">
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp">
            <ImageButton
                    style="@style/btnStyle"
                    android:layout_marginLeft="8dp"
                    app:srcCompat="@drawable/animal1"
                    android:id="@+id/imageButton4"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal3"
                    android:id="@+id/imageButton6"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal2"
                    android:id="@+id/imageButton5"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal4"
                    android:id="@+id/imageButton7"/>
        </LinearLayout>
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp">
            <ImageButton
                    style="@style/btnStyle"
                    android:layout_marginLeft="8dp"
                    app:srcCompat="@drawable/animal3"
                    android:id="@+id/imageButton8"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal3"
                    android:id="@+id/imageButton9"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal4"
                    android:id="@+id/imageButton10"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal2"
                    android:id="@+id/imageButton11"/>
        </LinearLayout>
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp">
            <ImageButton
                    style="@style/btnStyle"
                    android:layout_marginLeft="8dp"
                    app:srcCompat="@drawable/animal1"
                    android:id="@+id/imageButton12"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal2"
                    android:id="@+id/imageButton13"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal2"
                    android:id="@+id/imageButton14"/>
            <ImageButton
                    style="@style/btnStyle"
                    app:srcCompat="@drawable/animal3"
                    android:id="@+id/imageButton15"/>
        </LinearLayout>
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp">
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

运行结果:

三、相对布局

通过相对定位的方式指定子控件的位置。

1、基本语法格式
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

</RelativeLayout>
2、自身常用属性
属性名称功能描述
android:layout_centerlnParent设置当前控件位于父布局的中央位置
android:layout_centerVertical设置当前控件位于父布局的垂直居中位置
android:layout_centerHorizontal设置当前控件位于父控件的水平居中位置
android:layout_above设置当前控件位于某空间上方
android:layout_below设置当前控件位于某空间下方
android:layout_toLeftOf设置当前控件位于某空间左侧
android:layout_toRightOf设置当前控件位于某空间右侧
android:layout_alignParentTop设置当前控件与父控件顶端对齐
android:layout_alignParentLeft设置当前控件与父控件左对齐
android:layout_alignParentRight设置当前控件与父控件右对齐
android:layout_alignParentBottom设置当前控件与父控件底端对齐
android:layout_alignTop设置当前控件上边界与某控件上边界对齐
android:layout_alignBottom设置当前控件下边界与某控件下边界对齐
android:layout_alignLeft设置当前控件左边界与某控件左边界对齐
android:layout_alignRight设置当前控件右边界与某控件右边界对齐

区分:android:layout_toLeftOf与android:layout_alignLeft

3、实战演练

仿写一个登录页面。

注意事项:

1、res/values/colors.xml需加入蓝色与红色两个颜色的定义(默认只有黑白)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="red">#ff0000</color>
    <color name="blue">#00a0ff</color>
</resources>

2、<为特殊符号,直接写入无法识别,需用   &#060;  代替

layout1完整代码:

<RelativeLayout 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"
                android:background="@drawable/loginbackground"
>

    <TextView
            android:text="登录"
            android:textSize="20dp"
            android:textColor="@color/white"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/textView3"/>
    <TextView
            android:text="&#060;"
            android:textSize="20dp"
            android:textColor="@color/white"
            android:layout_alignBottom="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/textView4"/>
    <ImageView
            android:layout_below="@+id/textView3"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:srcCompat="@drawable/logintouxiang" android:id="@+id/imageView"/>
    <EditText
            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:text="用户名"
            android:ems="15"
            android:id="@+id/editTextText"/>
    <ImageView
            android:layout_toLeftOf="@+id/editTextText"
            android:layout_alignTop="@+id/editTextText"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:srcCompat="@drawable/loginaccount2" android:id="@+id/imageView2"/>
    <EditText
            android:layout_below="@+id/editTextText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="15"
            android:id="@+id/editTextTextPassword"/>
    <ImageView
            android:layout_toLeftOf="@+id/editTextTextPassword"
            android:layout_alignTop="@+id/editTextTextPassword"
            android:layout_marginTop="5dp"
            android:layout_marginRight="3dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:srcCompat="@drawable/loginpassword" android:id="@+id/imageView3"/>
    <TextView
            android:text="□记住密码"
            android:textColor="@color/white"
            android:layout_below="@+id/editTextTextPassword"
            android:layout_alignLeft="@id/editTextTextPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/textView"/>
    <TextView
            android:text="忘记密码?"
            android:textColor="@color/red"
            android:layout_below="@+id/editTextTextPassword"
            android:layout_alignRight="@id/editTextTextPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/textView2"/>
    <Button
            android:text="登录"
            android:backgroundTint="@color/blue"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="10dp"
            android:layout_width="80dp"
            android:layout_height="wrap_content" android:id="@+id/button"/>
    <TextView
            android:text="————其他登录方式————"
            android:layout_below="@id/button"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/textView5"/>
    <ImageView
            android:layout_below="@+id/textView5"
            android:layout_marginTop="8dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:srcCompat="@drawable/loginqq" android:id="@+id/imageView4"/>
    <ImageView
            android:layout_below="@+id/textView5"
            android:layout_marginTop="8dp"
            android:layout_marginRight="10dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:srcCompat="@drawable/loginwechat" android:id="@+id/imageView5"/>
    <ImageView
            android:layout_below="@+id/textView5"
            android:layout_marginTop="8dp"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:srcCompat="@drawable/loginzhi" android:id="@+id/imageView6"/>
</RelativeLayout>

运行结果:

四、表格布局

采用行、列的形式管理控件,不需要明确声明其中包含多少行、多少列,是通过在表格中添加TableRow布局或控件来控制表格的行数,通过在TableRow布局中添加控件来控制表格列数。

1、基本语法格式
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TableRow>
        UI控件
    </TableRow>
    ...
</TableLayout>
2、自身常用属性

由于TableLayout继承自LinearLayout,因此它完全支持LinearLayout所支持的属性,除此以外它还有以下几个属性。

属性名称功能描述
android:stretchColumns设置可拉伸的列
android:shrinkColumns设置可收缩的列
android:collapseColumns设置可隐藏的列

例:android:stretchColumns=“0”表示第一列可以拉伸

       android:shrinkColumns=“1,2”表示第2,3列可以收缩

3、TableLayout的控件的属性
控件属性功能描述
android:layout_column设置控件显示的位置
android:layout_span设置控件占据的列数,默认为1

例:android:layout_column=“1”表示在第2个位置显示

4、实战演练
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_column="0"
            android:text="按钮1"
        />
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_column="1"
                android:text="按钮2"
        />
    </TableRow>
    <TableRow>
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_column="1"
                android:text="按钮3"
        />
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_column="2"
                android:text="按钮4"
        />
    </TableRow>
    <TableRow>
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_column="2"
                android:text="按钮5"
        />
    </TableRow>
</TableLayout>

运行结果:

五、帧布局

帧布局用于在平面上创建一块空白区域,添加到该区域的每一个子控件占一帧,这些帧会一个个叠加在一起,后加入的控件会叠加在上一个控件的上层。默认情况下,帧布局的所有控件会与布局的左上角对齐。

1、基本语法格式
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
</FrameLayout>
2、自身常用属性
属性名称功能描述
android:foreground

设置FrameLayout容器的前景图像

(始终在所有子控件之上)

android:foregroundGravity设置前景图像显示位置

  • 28
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android开发教程及笔记-完整版.pdf》是一本关于Android开发的教程和笔记的完整版电子书。这本电子书主要介绍了Android开发所需的各种知识和技术,对于想要学习Android开发的人来说是一本非常有价值的资料。 这本电子书包含了Android开发的基础知识,如Android系统的介绍、Android开发环境的搭建以及常用开发工具的使用方法。同时,它也详细介绍了Android应用程序的开发流程,包括界面设计、布局管理、事件处理、数据库操作等方面的内容,使读者能够全面掌握Android应用程序的开发技巧。 此外,这本电子书还介绍了一些高级的Android开发技术,如网络编程、多媒体处理、传感器应用等方面的知识。通过学习这些高级技术,读者可以进一步提升自己的Android开发水平,设计出更加优秀和复杂的Android应用程序。 除了知识点的介绍之外,这本电子书还提供了大量的实例和代码,让读者能够通过实践来巩固所学知识。同时,它还给出了一些常见问题的解决方法和开发经验的分享,帮助读者更好地理解和应用所学的知识。 总之,《Android开发教程及笔记-完整版.pdf》是一本非常实用的Android开发学习资料,其全面而详细的内容将帮助读者系统地学习和掌握Android开发的技能,为实际项目的开发提供了很好的指导。无论是初学者还是有一定经验的开发者,都可以从中受益匪浅。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值