Android 实现控件对称布局(约束布局和线性布局)

news2024/9/24 3:29:55

        画界面时会遇到很多界面上的布局,虽然很简单,但是每次做起来不熟练,总结一下一些日常的

一.实现界面上的两个空间对称布局

方法一、用约束布局的guideLine.适用于两个控件不确定宽高,且约束条件较多

Guideline是只能用在ConstraintLayout布局里面的一个工具类,用于辅助布局,类似为辅助线,可以设置android:orientation属性来确定是横向的还是纵向的。

重要属性:

  • layout_constraintGuide_begin,指定左侧或顶部的固定距离,如10dp,在距离左侧或者顶部10dp的位置会出现一条辅助线
  • layout_constraintGuide_end,指定右侧或底部的固定距离,如50dp,在距离右侧或底部50dp的位置会出现一条辅助线
  • layout_constraintGuide_percent,指定在父控件中的宽度或高度的百分比,如0.5,表示距离垂直或者水平居中。

 

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:textColor="@color/black"
                android:text="guideline 实现对称布局"/>

            <androidx.appcompat.widget.AppCompatButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定"
                android:layout_marginTop="30dp"
                android:layout_marginEnd="20dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toStartOf="@id/guide_line_1"
                android:textColor="@color/white"
                android:background="@drawable/rgb1a1c1f_r8"
                android:textStyle="bold"
                android:paddingHorizontal="20dp"/>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guide_line_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.5"/>

            <androidx.appcompat.widget.AppCompatButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="取消"
                android:layout_marginTop="30dp"
                android:background="@drawable/rgb1a1c1f_r8"
                android:layout_marginStart="20dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toEndOf="@id/guide_line_1"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:paddingHorizontal="20dp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

方法二:线性布局,都知道线性布局要么水平要么垂直,当要求其子控件均匀排列,或者按一定比例占据父容器的宽高时,可用到其weight属性 

这是实现左右对称

            <LinearLayout
                android:paddingHorizontal="20dp"
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/ll_1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/cl_1"
                android:orientation="horizontal">
                
                <androidx.appcompat.widget.AppCompatButton
                    android:background="@drawable/rgb1a1c1f_r8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="20dp"
                    android:text="@string/video"
                    android:layout_weight="1"/>
                <androidx.appcompat.widget.AppCompatButton
                    android:background="@drawable/rgb1a1c1f_r8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:text="游戏"
                    android:layout_weight="1"/>
                
                
            </LinearLayout>

这是实现weight比例:

 

 

            <LinearLayout
                android:paddingHorizontal="10dp"
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/ll_1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/cl_1"
                android:orientation="horizontal">

                <androidx.appcompat.widget.AppCompatButton
                    android:background="@drawable/rgb1a1c1f_r8"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:text="@string/video"
                    android:layout_weight="1"/>
                <androidx.appcompat.widget.AppCompatButton
                    android:background="@drawable/rgb1a1c1f_r8"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:text="游戏"
                    android:layout_weight="1"/>
                <androidx.appcompat.widget.AppCompatButton
                    android:background="@drawable/rgb1a1c1f_r8"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:text="小说"
                    android:layout_weight="4"/>


            </LinearLayout>

很基础的知识,做个人总结用。

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

linux安装并发送邮件

linux安装、配置并发送邮件&#xff08;以CentOS7.9为例&#xff09; 1、安装邮箱软件 yum install mailx -y2、 配置邮箱&#xff08;以qq邮箱为例&#xff09; 2.1 网页访问并登录&#xff1a;https://mail.qq.com/ 2.2 选择“设置->账户” 2.3 在账户下面找到POP3/IMAP/…

【广州华锐互动】VR数字虚拟展厅为企业提升品牌形象和知名度

VR数字虚拟展厅是一种利用虚拟现实技术来展示企业产品和服务的全新宣传方式&#xff0c;与传统展厅相比具有出色的互动功能和沉浸体验感&#xff0c;参观者可以随时随地进入虚拟环境中进行参观&#xff0c;感受全新视听觉的体验。 VR数字虚拟展厅能够带来很多优势和好处&#x…

Flink 优化(六) --------- FlinkSQL 调优

目录一、设置空闲状态保留时间二、开启 MiniBatch三、开启 LocalGlobal四、开启 Split Distinct五、多维 DISTINCT 使用 Filter六、设置参数总结FlinkSQL 官网配置参数&#xff1a; https://ci.apache.org/projects/flink/flink-docs-release-1.13/dev/table/config.html 一、…

Zookeeper源码分析——算法基础

Zookeeper高级 Paxos 算法 Paxos算法&#xff1a;一种基于消息传递且具有高度容错特性的一致性算法。 Paxos算法解决的问题&#xff1a;就是如何快速正确的在一个分布式系统中对某个数据值达成一致&#xff0c;并且保证不论发生任何异常&#xff0c; 都不会破坏整个系统的一…

回溯递归(例题+思路+代码)

题目描述 leetcode 77 思路 组合问题适合用回溯求解。 经典解法&#xff1a;for循环 内部回溯。 每次进入回溯方法时&#xff0c;先判断终止条件&#xff0c;再进行当前层的循环&#xff0c;循环进行下一层递归。 代码 class Solution {public List<List<Integer&…

【C++入门必备知识:缺省参数+函数重载+函数名修饰规则】

【C入门必备知识&#xff1a;缺省参数函数重载函数名修饰规则】 ①.缺省参数Ⅰ.概念1.全缺省参数2.半缺省参数3.使用规则4.应用场景再现 ②.函数重载Ⅰ.概念1.参数个数不同2.参数类型不同3.参数类型顺序不同4.对返回值没有要求 ③.函数名修饰规则Ⅰ.C/C的不同 ①.缺省参数 Ⅰ.…

ubuntu22.04安装pyCUDA

更多内容请查看 www.laowubiji.com 笔者近期想使用GPU进行并行计算&#xff0c;搜索之后看到需要用到pyCUDA库函数&#xff0c;所以需要在所使用的ubuntu22.04系统中部署pyCUDA库&#xff0c;没想到在部署过程中折腾了好几回&#xff0c;总算是安装成功了。简单记录过程如下&a…

从输入URL到页面展示到底发生了什么

刚开始写这篇文章还是挺纠结的&#xff0c;因为网上搜索“从输入url到页面展示到底发生了什么”&#xff0c;你可以搜到一大堆的资料。而且面试这道题基本是必考题&#xff0c;二月份面试的时候&#xff0c;虽然知道这个过程发生了什么&#xff0c;不过当面试官一步步追问下去的…

AI智能改写-文本改写人工智能

随着信息技术的不断发展&#xff0c;互联网上各种信息的海量涌现&#xff0c;万千信息竞相呈现&#xff0c;如何让自己的内容独领风骚&#xff0c;引起用户的注意和眼球&#xff1f;这时&#xff0c;一款强大的文章智能改写神器便应运而生&#xff0c;可以让您的内容变得更加独…

可能你已经刷了很多01背包的题,但是真的对01背包领悟透彻了吗?,看我这一篇,使君对01背包的理解更进一步【代码+图解+文字描述】

一.概念理解&#xff1a;什么是01背包 关于01背包的概念理解如下&#xff1a;01背包是在M件物品取出若干件放在空间为W的背包里&#xff0c;每件物品的体积为W1&#xff0c;W2至Wn&#xff0c;与之相对应的价值为P1,P2至Pn。001背包的约束条件是给定几种物品&#xff0c;每种物…

SpringAMQP的使用

目录一、什么是SpringAMQP二、基本消息队列消息发送消息接收三、WorkQueue队列四、发布订阅模型FanoutExchangeDirectExchangeTopicExchange五、消息转换器一、什么是SpringAMQP 它可以大大的简化我们的开发&#xff0c;不用我们再自己创建连接写一堆代码&#xff0c;具有便捷…

【MySQL--05】表的约束

文章目录 1.表的约束1.1空属性1.2默认值default vs null1.3列描述1.4 zerofill1.5主键primary key1.6 自增长auto_increment1.7唯一键 unique如何设计主键&#xff1f;1.8 外键 foreign key 1.表的约束 真正的约束字段的是数据类型&#xff0c;但是数据类型约束很单一&#xf…

安捷伦E4405B

18320918653 E4405B E4405B|Agilent|ESA-E系列|10G|频谱分析仪|9kHz至13.2GHz 安捷伦 Agilent 惠普 HP 测量速度&#xff1a;28次更新/秒 测量精度&#xff1a;1dB 可选用的10Hz分辨事宽滤波器 机箱可容纳6插槽选件卡 97dB三阶动态范围 能在现场使用的坚固&#xff0c…

mycat2安装配置,mycat2分库分表,mycat2一库多表,mycat2自增id

1、官网下载&#xff08;官网下载地址&#xff09; 官网下载地址 Index of /2.0/ 下载模板 下载jdk包 下载好后吧jdk包房到mycat的lib目录下 2、配置启动 配置结构 mycat配置文件夹 clusters- prototype.cluster.json //无集群的时候自动创建- c0.cluster.json- c1.cluster…

UML与代码的对应关系

五种关系的耦合强弱比较&#xff1a;依赖<关联<聚合<组合<继承 依赖 虚线箭头 可描述为&#xff1a;Uses a 依赖是类的五种关系中耦合最小的一种关系。 因为在生成代码的时候&#xff0c;这两个关系类都不会增加属性。 注意1&#xff1a; Water类的生命期&…

【机器学习】独立成分分析(ICA)及Matlab实现

独立成分分析及Matlab实现1.问题引入2.ICA原理3.ICA算法步骤4.性质与优点5.程序代码6.程序分析7.运行结果1.问题引入 独立成分分析&#xff08;ICA&#xff09;最初由Aapo Hyvrinen等人于1980年代提出&#xff0c;其起源可以追溯到对神经科学和信号处理领域的研究需求。ICA的提…

C语言判断一个日期是在该年的第几天案例讲解

今天是2023年4月11号&#xff0c;我们就用今天举例得出是2023年的第几天。 思路分析 1&#xff09;我们想知道2023年4月11号是2023年的第几天&#xff0c;只需要把1到3月份的天数累加求和然后加上今天日期也就是11就可以算出2023年4月11号是2023年的第几天。 推广&#xff1a;…

kafka集群节点重启后未被topic识别

1.案例 kafka集群的节点重启后,topic为apex的主题识别不到重启后的broker节点id,但是还能识别到副本集还在原来的broker节点上 在kafka manager上查看 继续往下查看 2.查看kafka日志报错原因 以下是两个不同的broker节点报错的报错日志 tail -f /etc/kafka/kafka/logs/ka…

(排序9)非比较排序之计数排序

非比较排序之计数排序 之前讲的七种排序方法的话&#xff0c;都是比较排序&#xff1b;除此之外还有三种非比胶排序&#xff1a;计数排序&#xff0c;基数排序&#xff0c;桶排序。后面两个实际应用没啥&#xff0c;没啥价值。非比较排序的话&#xff0c;他的条件都比较苛刻&a…

HTTP 和 HTTPS(请求响应报文格式 + 请求方法 + 响应状态码 + HTTPS 加密流程 + Cookie 和 Session)

文章目录1. HTTP 是什么2. HTTP 请求报文和响应报文的格式1&#xff09;请求报文格式2&#xff09;响应报文格式3&#xff09;报文中空行的作用3. HTTP 的长连接和短连接4. URL1&#xff09;在浏览器中输入 www.baidu.com 后执行的全部过程5. HTTP 常用的请求方法6. GET 和 POS…