当前位置: 首页 > news >正文

【Android】布局文件layout.xml文件使用控件属性android:layout_weight使布局较为美观,以RadioButton为例

目录

  • 说明
  • 举例

说明

  简单来说,android:layout_weight为当前控件按比例分配剩余空间。且单个控件该属性的具体数值不重要,而是多个控件的属性值之比发挥作用,例如有2个控件,各自的android:layout_weight的值设为0.5和0.5,或者设置为5和5,布局效果都是各占一半,都是1:1

举例

  本文以RadioButton为例,使用此控件需要搭配RadioGroup,初始版本代码如下:

<RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal">        <RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="仅通知"android:textSize="20dp"android:textColor="@android:color/black"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自动化"android:textSize="20dp"android:textColor="@android:color/black"/>
</RadioGroup>

  实现效果如下:
在这里插入图片描述
  喜欢对称放在屏幕中间风格的,给两个RadioButton设置android:layout_weight值为0.5,代码不再赘述,效果图如下:
在这里插入图片描述
  感觉还是不够对称的,希望能够在屏幕两侧有对称的空余,可以随便加些控件如TextView来占个位置,注意,占空控件的宽、高属性android:layout_widthandroid:layout_height设置为wrap_content或者0dp,且RadioButton不设置android:layout_weight属性。 这样占空控件只会分配RadioButton布局好后的剩余空间。代码补充如下:

<RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="0dp"android:layout_weight="3"android:layout_gravity="center"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="仅通知"android:textSize="20dp"android:textColor="@android:color/black"/><TextViewandroid:layout_width="0dp"android:layout_height="0dp"android:layout_weight="3"android:layout_gravity="center"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自动化"android:textSize="20dp"android:textColor="@android:color/black"/><TextViewandroid:layout_width="0dp"android:layout_height="0dp"android:layout_weight="3"android:layout_gravity="center"/>
</RadioGroup>

  最终效果如下:
在这里插入图片描述

http://www.lryc.cn/news/527256.html

相关文章:

  • RabbitMQ 架构分析
  • Qt Enter和HoverEnter事件
  • 大语言模型之prompt工程
  • WPF基础 | WPF 常用控件实战:Button、TextBox 等的基础应用
  • [笔记] 极狐GitLab实例 : 手动备份步骤总结
  • 随笔十七、eth0单网卡绑定双ip的问题
  • 逻辑复制parallel并发参数测试
  • Cursor 帮你写一个小程序
  • WordPress免费证书插件
  • Linux:多线程[2] 线程控制
  • C++——list的了解和使用
  • Agent群舞,在亚马逊云科技搭建数字营销多代理(Multi-Agent)(下篇)
  • DBeaver连接MySQL数据库
  • Leetcode40: 组合总和 II
  • win32汇编环境,对话框程序中使用进度条控件
  • AIGC时代下的Vue组件开发深度探索
  • 在Kubernets Cluster中部署LVM类型的StorageClass - 上
  • 一次StarRocks分析的经历
  • Django网站搭建流程
  • Vue-day2
  • Day44:列表元素的修改
  • 在 AMD GPU 上使用 vLLM 的 Triton 推理服务器
  • day7手机拍照装备
  • HarmonyOS:创建应用静态快捷方式
  • [SUCTF 2018]MultiSQL1
  • kafka-部署安装
  • VUE3 使用路由守卫函数实现类型服务器端中间件效果
  • |Python新手小白中级教程|第二十九章:面向对象编程(Python类的拓展延伸与10道实操题目)(5)
  • 项目概述与规划 (I)
  • mysql学习笔记-数据库的设计规范