Android学习之路(3) 布局

线性布局LinearLayout

前几个小节的例程中,XML文件用到了LinearLayout布局,它的学名为线性布局。顾名思义,线性布局 像是用一根线把它的内部视图串起来,故而内部视图之间的排列顺序是固定的,要么从左到右排列,要 么从上到下排列。在XML文件中,LinearLayout通过属性android:orientation区分两种方向,其中从左 到右排列叫作水平方向,属性值为horizontal;从上到下排列叫作垂直方向,属性值为vertical。如果LinearLayout标签不指定具体方向,则系统默认该布局为水平方向排列,也就是默认android:orientation="horizontal".

下面做个实验,让XML文件的根节点挂着两个线性布局,第一个线性布局采取horizontal水平方向,第 二个线性布局采取vertical垂直方向。然后每个线性布局内部各有两个文本视图,通过观察这些文本视图 的排列情况,从而检验线性布局的显示效果。详细的XML文件内容如下所示:


<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="wrap_content"
 
        android:orientation="horizontal">
 
        <TextView
 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
 
            android:text="横排第一个"
 
            android:textSize="17sp"
 
            android:textColor="#000000" />
 
        <TextView
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:text="横排第二个"
 
            android:textSize="17sp"
 
            android:textColor="#000000" />
 
    </LinearLayout>
 
    <LinearLayout
 
        android:layout_width="match_parent"
 
        android:layout_height="wrap_content"
 
        android:orientation="vertical">
 
        <TextView
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:text="竖排第一个"
 
            android:textSize="17sp"
 
            android:textColor="#000000" />
 
        <TextView
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:text="竖排第二个"
 
            android:textSize="17sp"
 
            android:textColor="#000000" />
 
    </LinearLayout>
</LinearLayout>

运行测试App,进入如下图所示的演示页面,可见horizontal为横向排列,vertical为纵向排列,说明android:orientation的方向属性确实奏效了。

除了方向之外,线性布局还有一个权重概念,所谓权重,指的是线性布局的下级视图各自拥有多大比例 的宽高。比如一块蛋糕分给两个人吃,可能两人平均分,也可能甲分三分之一,乙分三分之二。两人平 均分的话,先把蛋糕切两半,然后甲分到一半,乙分到另一半,此时甲乙的权重比为1:1。甲分三分之 一、乙分三分之二的话,先把蛋糕平均切成三块,然后甲分到一块,乙分到两块,此时甲乙的权重比为1:2。就线性布局而言,它自身的尺寸相当于一整块蛋糕,它的下级视图们一起来分这个尺寸蛋糕,有的 视图分得多,有的视图分得少。分多分少全凭每个视图分到了多大的权重,这个权重在XML文件中通过 属性android:layout_weight来表达。

把线性布局看作蛋糕的话,分蛋糕的甲乙两人就相当于线性布局的下级视图。假设线性布局平均分为左 右两块,则甲视图和乙视图的权重比为1:1,意味着两个下级视图的layout_weight属性都是1。不过视图 有宽高两个方向,系统怎知layout_weight表示哪个方向的权重呢?所以这里有个规定,一旦设置了layout_weight属性值,便要求layout_width填0dp或者layout_height填0dp。如果layout_width填0dp,则layout_weight表示水平方向的权重,下级视图会从左往右分割线性布局;如果layout_height填0dp,则layout_weight表示垂直方向的权重,下级视图会从上往下分割线性布局。 按照左右均分的话,线性布局设置水平方向horizontal,且甲乙两视图的layout_width都填0dp,layout_weight都填1,此时横排的XML片段示例如下:

<LinearLayout
 
        android:layout_width="match_parent"
 
        android:layout_height="wrap_content"
 
        android:orientation="horizontal">
 
    <TextView
 
              android:layout_width="0dp"
 
              android:layout_height="wrap_content"
 
              android:layout_weight="1"
 
              android:text="横排第一个"
 
              android:textSize="17sp"
 
              android:textColor="#000000" />
 
    <TextView
 
              android:layout_width="0dp"
 
              android:layout_height="wrap_content"
 
              android:layout_weight="1"
 
              android:text="横排第二个"
 
              android:textSize="17sp"
 
              android:textColor="#000000" />
</LinearLayout>

按照上下均分的话,线性布局设置垂直方向vertical,且甲乙两视图的layout_height都填0dp,layout_weight都填1,此时竖排的XML片段示例如下:

<LinearLayout
 
        android:layout_width="match_parent"
 
        android:layout_height="wrap_content"
 
        android:orientation="vertical">
 
    <TextView
 
              android:layout_width="wrap_content"
 
              android:layout_height="0dp"
 
              android:layout_weight="1"
 
              android:text="竖排第一个"
 
              android:textSize="17sp"
 
              android:textColor="#000000" />
 
    <TextView
 
              android:layout_width="wrap_content"
 
              android:layout_height="0dp"
 
              android:layout_weight="1"
 
              android:text="竖排第二个"
 
              android:textSize="17sp"
 
              android:textColor="#000000" />
</LinearLayout>

把上面两个片段放到新页面的XML文件,其中第一个是横排区域采用红色背景(色值为ff0000),第二 个是竖排区域采用青色背景(色值为00ffff)。重新运行测试App,打开演示界面如下图所示,可见横 排区域平均分为左右两块,竖排区域平均分为上下两块。

相对布局RelativeLayout

线性布局的下级视图是顺序排列着的,另一种相对布局的下级视图位置则由其他视图决定。相对布局名 为RelativeLayout,因为下级视图的位置是相对位置,所以得有具体的参照物才能确定最终位置。如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。 用于确定下级视图位置的参照物分两种,一种是与该视图自身平级的视图;另一种是该视图的上级视图 (也就是它归属的RelativeLayout)。综合两种参照物,相对位置在XML文件中的属性名称说明见下表。

为了更好地理解上述相对属性的含义,接下来使用RelativeLayout及其下级视图进行布局来看看实际效果图。下面是演示相对布局的XML文件例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:layout_width="match_parent"
 
    android:layout_height="150dp" >
 
        <TextView
 
        android:id="@+id/tv_center"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_centerInParent="true"
 
        android:background="#ffffff"
 
        android:text="我在中间"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_center_horizontal"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_centerHorizontal="true"
 
        android:background="#eeeeee"
 
        android:text="我在水平中间"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_center_vertical"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_centerVertical="true"
 
        android:background="#eeeeee"
 
        android:text="我在垂直中间"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_parent_left"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_alignParentLeft="true"
 
        android:background="#eeeeee"
 
        android:text="我跟上级左边对齐"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    <TextView
 
        android:id="@+id/tv_parent_right"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_alignParentRight="true"
 
        android:background="#eeeeee"
 
        android:text="我跟上级右边对齐"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    <TextView
 
        android:id="@+id/tv_parent_top"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_alignParentTop="true"
 
        android:background="#eeeeee"
 
        android:text="我跟上级顶部对齐"
 
        android:textSize="11sp"
        android:textColor="#000000" />
 
    <TextView
 
        android:id="@+id/tv_parent_bottom"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_alignParentBottom="true"
 
        android:background="#eeeeee"
 
        android:text="我跟上级底部对齐"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_left_center"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_toLeftOf="@+id/tv_center"
 
        android:layout_alignTop="@+id/tv_center"
 
        android:background="#eeeeee"
 
        android:text="我在中间左边"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_right_center"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_toRightOf="@+id/tv_center"
 
        android:layout_alignBottom="@+id/tv_center"
 
        android:background="#eeeeee"
 
        android:text="我在中间右边"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_above_center"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_above="@+id/tv_center"
 
        android:layout_alignLeft="@+id/tv_center"
 
        android:background="#eeeeee"
 
        android:text="我在中间上面"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
 
    
    <TextView
 
        android:id="@+id/tv_below_center"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:layout_below="@+id/tv_center"
 
        android:layout_alignRight="@+id/tv_center"
 
        android:background="#eeeeee"
 
        android:text="我在中间下面"
 
        android:textSize="11sp"
 
        android:textColor="#000000" />
</RelativeLayout>

上述XML文件的布局效果如下图所示,RelativeLayout的下级视图都是文本视图,控件上的文字说明 了所处的相对位置,具体的控件显示方位正如XML属性中描述的那样。

Java程序如下:

package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
 
public class RelativeLayoutActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relative_layout);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/74052.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

前后端分离------后端创建笔记(03)前后端对接(下)

本文章转载于【SpringBootVue】全网最简单但实用的前后端分离项目实战笔记 - 前端_大菜007的博客-CSDN博客 仅用于学习和讨论&#xff0c;如有侵权请联系 源码&#xff1a;https://gitee.com/green_vegetables/x-admin-project.git 素材&#xff1a;https://pan.baidu.com/s/…

Java【数据结构】二分查找

&#x1f31e; 题目&#xff1a; &#x1f30f;在有序数组A中&#xff0c;查找目标值target &#x1f30f;如果找到返回索引 &#x1f30f;如果找不到返回-1 算法描述解释前提给定一个内含n个元素的有序数组A&#xff0c;满足A0<A1<A2<<An-1,一个待查值target1设…

游戏中的UI适配

引用参考&#xff1a;感谢GPT UI适配原理以及常用方案 游戏UI适配是确保游戏界面在不同设备上以不同的分辨率、屏幕比例和方向下正常显示的关键任务。下面是一些常见的游戏UI适配方案&#xff1a; 1.分辨率无关像素&#xff08;Resolution-Independent Pixels&#xff09;&a…

【EI/SCOPUS检索】第三届计算机视觉、应用与算法国际学术会议(CVAA 2023)

第三届计算机视觉、应用与算法国际学术会议&#xff08;CVAA 2023) The 3rd International Conference on Computer Vision, Application and Algorithm 2023年第三届计算机视觉、应用与算法国际学术会议&#xff08;CVAA 2023&#xff09;主要围绕计算机视觉、计算机应用、计…

Python程序设计基础:函数(二)

文章目录 一、lambda()函数二、递归函数三、变量的作用域 一、lambda()函数 lambda()函数是一种简便的&#xff0c;将函数定义在同一行的函数方法。lambda()实际上生成了一个函数对象&#xff08;匿名函数&#xff09;&#xff0c;它主要用于需要函数对象作为参数或函数比较简…

ChatGPT能代替搜索引擎吗?ChatGPT和搜索引擎有什么区别?

ChatGPT和搜索引擎是两种在信息获取和交流中常用的工具&#xff0c;ChatGPT是一种基于人工智能技术的聊天机器人&#xff0c;而搜索引擎是一种在互联网上搜索信息的工具。尽管它们都是依托互联网与信息获取和交流有关&#xff0c;部分功能重合&#xff0c;但在很多方面存在着明…

并发编程 - 线程池中的常见面试题

目录 1. 线程池相比于线程有什么优点 2. 线程池的参数有哪些 3. 线程工厂有什么用 4. 说一下线程的优先级 5. 说一下线程池的执行流程 6. 线程池的拒绝策略有哪些 7. 如何实现自定义拒绝策略 8. 如何判断线程池中的任务是否执行完成 1. 线程池相比于线程有什么优点 有…

2023.8.14论文阅读

文章目录 ESPNet: Efficient Spatial Pyramid of Dilated Convolutions for Semantic Segmentation摘要本文方法实验结果 DeepFusion: Lidar-Camera Deep Fusion for Multi-Modal 3D Object Detection摘要本文方法实验结果 ESPNet: Efficient Spatial Pyramid of Dilated Convo…

ASR 语音识别接口封装和分析

这个文档主要是介绍一下我自己封装了 6 家厂商的短语音识别和实时流语音识别接口的一个包&#xff0c;以及对这些接口的一个对比。分别是&#xff0c;阿里&#xff0c;快商通&#xff0c;百度&#xff0c;腾讯&#xff0c;科大&#xff0c;字节。 zxmfke/asrfactory (github.c…

Stable Diffusion + AnimateDiff运用

1.安装AnimateDiff&#xff0c;重启webui 2.下载对应的模型&#xff0c;最好到c站下载&#xff0c;google colab的资源有可能会出现下载问题 https://civitai.com/models/108836 3.下载完成后&#xff0c;你可以随便抽卡了。 抽卡完成后固定seed&#xff0c;然后打开这个插件&…

Docker安装elasticsearch分布式搜索

文章目录 ☀️安装elasticsearch☀️1.部署单点es&#x1f338;1.1.创建网络&#x1f338;1.2.下载镜像&#x1f338;1.3.运行 ☀️2.部署kibana&#x1f338;2.1.部署&#x1f338;2.2.DevTools ☀️3.安装IK分词器&#x1f338;3.1.在线安装ik插件&#xff08;较慢&#xff0…

【报错】ModuleNotFoundError: No module named ‘websocket‘

1 报错 ModuleNotFoundError: No module named websocket 2 解决方法 pip install websocket 1 报错 AttributeError: module websocket has no attribute enableTrace 2 分析 一般是由于websocket的依赖包没有安装造成的。websocket.enableTrace()方法是在websocket-cli…

013 怎么激活win10系统?

1、搜索软件Windows PowerShell&#xff1a; 在“开始”菜单栏&#xff0c;搜索“Windows PowerShell.exe”,以管理员模式打开。 2、输入 三条命令&#xff1a; &#xff08;1&#xff09;slmgr /ipk VK7JG-NPHTM-C10JM-9MPGT-3A77T &#xff08;最后这个字符为秘钥&#xf…

Qt自定义对话框

介绍 自定义框主要通过对现有对话框QDialog类的派生&#xff0c;根据需求编写成员函数、重载信号函数、槽函数&#xff0c;进而实现在主QWidget中点击某个按钮后&#xff0c;一个对话框的弹出 流程 简化创建派生类 最后点击完成即可。 自定义ui界面&#xff0c;编写成员函数…

drawio导出矢量图

1.选中要导出的图 2.导出为pdf 3.用adobe打开pdf&#xff0c;另存为eps

【数据结构】“单链表”的练习题(二)

&#x1f490; &#x1f338; &#x1f337; &#x1f340; &#x1f339; &#x1f33b; &#x1f33a; &#x1f341; &#x1f343; &#x1f342; &#x1f33f; &#x1f344;&#x1f35d; &#x1f35b; &#x1f364; &#x1f4c3;个人主页 &#xff1a;阿然成长日记 …

spring security实践-全套代码

贴一套完整代码 电脑文件都被加密了&#xff0c;无法上传git&#xff0c;留一套在此&#xff0c;日后方便。 整个学习过程参考的spring security 1. 项目目录结构 2.初始化数据库 CREATE TABLE sys_user (id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 主键,user_name VAR…

欧拉OS 使用 CentOS 7 yum repo

一、下载CentOS的repo的yum文件 任何基于CentOS的yum的repo 的url是这样的&#xff1a; 但欧拉OS输出这个变量为&#xff1a;openEuler 20.03 (LTS-SP3) 那明显欧拉想要使用这个yum的url找不到这个版本&#xff0c; 所以直接讲这个变量替换为 7, Centos 7的7 然后执行&…

【设计模式】工厂模式

工厂模式 工厂模式&#xff08;Factory Pattern&#xff09;是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式&#xff0c;它提供了一种创建对象的最佳方式。 工厂模式提供了一种将对象的实例化过程封装在工厂类中的方式。通过使用工厂模式&#xff0c;可以…
最新文章