Android UI系列-----LinearLayout的综合使用
这里将会对LinearLayout的布局方式进行一个综合的使用,通过一个例子来看看LinearLayout的嵌套布局方式,在这之前首先介绍三个属性:
1.①android:layout_weigth:这个属性是用来控制子控件占据的比例的。如果一个父控件在摆放了子控件后,还有剩余空间,那么我们可以通过layout_weigth这个属性可以将剩余的空间按比例分配给子控件。注意:layout_weight 瓜分的是父控件的剩余空间,而不是瓜分整个父控件。
例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AC8720"
android:baselineAligned="false"
android:orientation="horizontal"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#ABCDEF"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="android"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="android"/>
</LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FEDCBA"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="java"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="java"/>
</LinearLayout> </LinearLayout>
小技巧:既然layout_weight瓜分的是父控件的剩余空间,那么我们如果要设置子控件占同样的空间比例的时候,可以将子控件的 layout_width 设置成 0dp,然后将layout_weigth设置成1,因为如果layout_width设置成0dp,则表示宽度为0,那么所有的空间都是剩余空间,然后设置layout_weigth=1,这样两个控件就占同样的空间了。即:
android:layout_width="0dp"
android:layout_weight=""
②android:layout_gravity:这个属性是用来设置控件的放置位置的,例如居中放置,居左放置等。
③android:gravity:这个属性是用来设置控件中字体的放置位置的,同样可以设置居中、居左等。
2.通过一个实例来熟悉一下LinearLayout的使用方法:

布局文件的代码如下:
<?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" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="猜拳游戏"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:scaleType="centerCrop"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪子"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:scaleType="centerCrop"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪子"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="确定"
android:textSize="30sp"/>
<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:gravity="center"
android:text="结果"
android:textSize="20sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="测试结果"
android:textSize="20sp"/>
</LinearLayout> </LinearLayout>
Android UI系列-----LinearLayout的综合使用的更多相关文章
- Android UI系列-----时间、日期、Toasts和进度条Dialog
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
- 【转】Android UI系列-----时间、日期、Toasts和进度条Dialog
原文网址:http://www.cnblogs.com/xiaoluo501395377/p/3421727.html 您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注 ...
- Android UI系列-----ScrollView和HorizontalScrollView
本篇随笔将讲解一下Android当中比较常用的两个布局容器--ScrollView和HorizontalScrollView,从字面意义上来看也是非常的简单的,ScrollView就是一个可以滚动的V ...
- Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)
一.Dialog介绍 dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一个对话框并不会沾满我们整个的屏幕,并且通常用于模型事件当中需要用户做出一个决定后才会继续 ...
- Android UI系列-----RelativeLayout的相关属性
本篇随笔将主要记录一些RelatieLayout的相关属性,并将猜拳游戏通过RelativeLayout实现出来 RelativeLayout的几组属性 第一组属性:android:layout_be ...
- Android UI系列-----CheckBox和RadioButton(1)
主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...
- Android UI系列-----长度单位和内外边距
这篇随笔将会记录一下在控件布局时,设定距离的三种长度单位:px.dp.sp以及内外边距的属性 1.三种长度单位 ①px:px是我们常见的一种距离单位,它表示的是一个单位像素,我们经常说我们手机或者电脑 ...
- Android UI之LinearLayout详解
※※※摘自http://www.cnblogs.com/salam/archive/2010/10/20/1856793.html LinearLayout是线性布局控件,它包含的子控件将以横向或竖向 ...
- Android UI系列-----Dialog对话框
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
随机推荐
- 【Java】 剑指offer(59-1) 滑动窗口的最大值
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值.例 ...
- MQ确认机制之事务机制------tx
一:介绍 1.介绍 在前面的说的模式中会出现一个问题. 就是生产者将消息发送出去到底有没有到达rabbitMq,默认情况下是不知道. 有两种解决方式. AMQP实现事务机制 Confirm机制. 这里 ...
- 093实战 Nginx日志切割,以及脚本上传nginx的切割日志
一:日志切割步骤 命令都在root下进行 1.创建目录 mkdir -p /etc/opt/modules/bin ## 创建文件夹 2.上传cut 3.观察目录 4.修改的cut文件 5.检测 需要 ...
- 洛谷 P1507 NASA的食物计划 【二维费用背包】 || 【DFS】
题目链接:https://www.luogu.org/problemnew/show/P1507 题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安全技术问题一直大伤脑筋,因此在各方压力 ...
- SpringMvc @ResponseBody
一.@Response使用条件 二. @Response在最小配置.jackson的jar包情况下,json中包含的日期类型字段都是以时间戳long类型返回 三. Jack序列化对象转为JSON的限制 ...
- CTFlearn Inj3ction Time --sql注入python多线程脚本练习
0x00前言: 本题是国外的一个CTF平台的题目,题目不难,但学习了波多线程payload写法 先看题目描述,提示"union是个有用的命令",估计是用联合查询就能出答案的(因为前 ...
- PushBackInputStream回退流
[例子1] import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PushbackInputS ...
- struts1 标签引入
1 tld文件导入 目录结构如下 2 jsp 文件头部标签引入 <%@ page pageEncoding="gbk" contentType="text/html ...
- phpstorm在项目中查找某个字符串
如果项目过大,想在整个项目中找某个字符串,又不知道该字符串所在文件的路径,这时候就可以使用全局搜索了,ctrl+shift+F.在mac下,快捷键是command+shift+F:
- Windows7的MySQL数据库的安装
碰巧重装了系统,需要重新安装MySQL. 1.进入官网下载:https://dev.mysql.com/ 2.severonle 3.alt+n 4.alt+x