首页 > 其他分享 >C004Android学习笔记-中级控件(一)

C004Android学习笔记-中级控件(一)

时间:2023-02-22 16:02:22浏览次数:35  
标签:控件 RelativeLayout C004Android 参照物 位置 视图 笔记 下级 图层

一、RelativeLayout相对布局

1、概述

①RelativeLayout下级视图的位置是相对位置,得有具体发参照物才能确定最终位置;

②如果不指定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角;

③用于确定视图位置的参照物分为两种,一种是与该视图平级的视图,另一种是该视图的上级视图;

 

2、相对位置的属性与类型的取值说明

C004Android学习笔记-中级控件(一)_相对布局

 

3、说明

相对布局的视图位置一般会在XML布局文件中定义好,特殊情况下在代码中定义相对位置用到的是RelativeLayout.LayoutParams的addRule方法,该方法第一个参数是相对位置的类型(见上表),第二个参数是参照物资源视图的ID;

 

4、代码演示

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="HardcodedText">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="同级参照"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上级水平居中"
        android:layout_centerHorizontal="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上级垂直居中"
        android:layout_centerVertical="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="上级左侧对齐"
        android:layout_alignParentStart="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="上级右侧对齐"
        android:layout_alignParentEnd="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上级顶部对齐"
        android:layout_alignParentTop="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上级底部对齐"
        android:layout_alignParentBottom="true"/>

    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图左边"
        android:layout_centerInParent="true"
        android:layout_toStartOf="@id/btn"/>

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图右边"
        android:layout_centerInParent="true"
        android:layout_toEndOf="@id/btn"/>

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图上边"
        android:layout_centerInParent="true"
        android:layout_above="@id/btn"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图下边"
        android:layout_centerInParent="true"
        android:layout_below="@id/btn"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图左对齐"
        android:layout_above="@id/btn1"
        android:layout_alignStart="@id/btn"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图右对齐"
        android:layout_below="@id/btn2"
        android:layout_alignEnd="@id/btn"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图右对齐顶部对齐"
        android:layout_alignTop="@id/btn3"
        android:layout_alignEnd="@id/btn5"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="参照视图右对齐底部对齐"
        android:layout_alignBottom="@id/btn3"
        android:layout_alignStart="@id/btn4"/>


</RelativeLayout>

效果:

C004Android学习笔记-中级控件(一)_图层_02

 

二、框架布局FrameLayout

1、概述

FrameLayout就像PS里面的图层一样,前面的视图在下级图层,后面的视图在上级图层;

 

2、常用属性

foreground:指定框架布局的前景图像,该图像在框架内部永远处于最顶层,不会被框架的其他视图覆盖;

foregroundGravity:指定前景图像的对其方式,取值通同Gravity;

 

3、代码演示

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="HardcodedText">

    <View
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#000000"/>
    <View
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#ff0000"/>
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ffff00"/>
    

</FrameLayout>

效果:

C004Android学习笔记-中级控件(一)_xml_03

 

 

 

 

 

 

 

 

 

标签:控件,RelativeLayout,C004Android,参照物,位置,视图,笔记,下级,图层
From: https://blog.51cto.com/u_13272819/6079195

相关文章

  • 《分布式技术原理与算法解析》学习笔记Day19
    分布式通信:消息队列什么是消息队列?队列是一种具有先进先出特点的数据结构,消息队列是基于队列实现的、存储具有特定格式的消息数据。消息以特定格式放入这个队列的尾部后......
  • E013Web学习笔记-Request和Respons(一)
    一、Request原理和继承体系1、原理第一步:用户访问url,浏览器向服务器发出请求,请求携带着请求消息数据;第二步:Tomcat服务器会根据请求url中的资源......
  • E010Web学习笔记-Tomcat
    一、web相关概念回顾1、软件架构C/S架构:客户端/服务器端;B/S架构:浏览器/服务端;浏览器内含静态资源解析引擎;浏览器请求数据,服务器响应来自浏览器的请求; 2、资源......
  • E011Web学习笔记-Servlet
    一、Servlet入门1、概述概念:运行在服务器上的小程序;实际上Servlet是一个接口,定义了Java类被浏览器访问到(或者说被Tomcat识别的)规则;使用:自定......
  • E012Web学习笔记-HTTP请求协议
    第一次复习时间:2022年3月27日11点05分一、概述1、概念HyperTextTransferProtocol超文本传输协议;2、传输协议定义了客户端和服务器端发送数据的格式;3、特......
  • E005Web学习笔记-JavaScript(三):BOM
    一、简单学习DOM1、控制(增删改查)HTML文档的内容; 2、代码:获取页面的标签(元素)对象Element;document.getElementById();//通过元素的ID获取元素......
  • E006Web学习笔记-JavaScript(四):DOM
    一、概述1、简介DocumentObjectModel文档对象模型;将标记语言文档的各个部分,封装为对象,可以使用这些对象,对标记语言文档进行CRUD(增删改查)的动......
  • E007Web学习笔记-JavaScript(五):JS事件
    一、概述1、事件概念某些组件被执行了某些操作后,触发某些代码的执行;事件:某些操作,如点击,双击,鼠标移动,键盘按下……; 2、事件源组件,如按钮、文......
  • E004Web学习笔记-JavaScript(二):JS对象
    一、Function1、概述是一个函数对象; 2、Function:函数(方法)对象①创建//1、方法1(不建议使用)varfun=newFunction(形参列表,方法体);示......
  • 读书笔记:价值投资.十二.公司的定性分析
     如果封仓十年封仓十年的思路是给企业定性,确定这是一家好公司."时间是优秀企业的朋友,平庸企业的敌人.你可能认为这个结论平淡无奇,但我是通过深刻的教训才......