android LinearLayout和RelativeLayout实现精确布局
先明确几个概念的区别:
padding margin:都是边距的含义,关键问题得明白是什么相对什么的边距
padding:是控件的内容相对控件的边缘的边距.
margin :是控件边缘相对父空间的边距
android:gravity是对该view 内容的限定.
比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个.
android:layout_gravity 是用来设置该view中的子view相对于父view的位置.
比如一个button 在linearlayout里,你想把该button放在靠左,靠右等位置就可以在linearlayout中通过该属性设置
XML 布局文件
[html] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical">
- <ImageView android:id="@+id/ivLogo"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/icon"
- android:paddingLeft="5dp" />
- <RelativeLayout android:id="@+id/rl_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:padding="10dp">
- <TextView android:id="@+id/tvApplicationName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="16dp" />
- </RelativeLayout>
- <RelativeLayout android:id="@+id/rl_score"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:padding="10dp">
- <TextView android:id="@+id/tvRating"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="5.0" />
- <RatingBar android:id="@+id/ratingbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:numStars="5"
- style="?android:attr/ratingBarStyleSmall"
- android:layout_below="@id/tvRating" />
- </RelativeLayout>
- </LinearLayout>
上面布局文件的效果图
上面的布局文件是一个ListView中的list_item布局,在一个ListView中显示所有的APK资源,每个资源项显示图标,名称及评分。
在listItem的最外层LinearLayout中加android:gravity="center_vertical",设定内容垂直居中显示。
在id为rl_score的RelativeLayout中设定android:layout_width="fill_parent"来填充剩余空间;
android:gravity="right"设定内容相对于rl_score右对齐;
android:padding="10dp"设定RelativeLayout中的内容相对RelativeLayout的边缘的边距为10dp。
这个布局虽然简单,但却是经常用到的。
android LinearLayout和RelativeLayout实现精确布局相关推荐
- 用android LinearLayout和RelativeLayout实现精确布局(转)
先明确几个概念的区别: padding margin都是边距的含义,关键问题得明白是什么相对什么的边距. padding是控件的内容相对控件的边缘的边距. margin是控件边缘相对父控件的边距 ...
- [转]用android LinearLayout和RelativeLayout实现精确布局
先明确几个概念的区别: padding margin都是边距的含义,关键问题得明白是什么相对什么的边距. padding是控件的内容相对控件的边缘的边距. margin是控件边缘相对父控件的边距 ...
- Android基础教程之五大布局对象------FrameLayout,LinearLayout,AbsoluteLayout,RelativeLayout,TableLayout...
2019独角兽企业重金招聘Python工程师标准>>> 大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的), ...
- Android 第六课 4种基本布局之LinearLayout和Relativelayout
看完控件,紧接着看布局,布局是可以来放置控件,管理控件的.布局里也可以嵌套布局. 我们新建项目UILayoutTest项目,活动名和布局名选择默认.加入活动及其对应的布局已经创建完成. 线性布局(Li ...
- Android - 布局详解之LinearLayout与RelativeLayout
本篇博文只针对LinearLayout与RelativeLayout 我们在新建一个布局文件时,一般遵循这样的思路:先确定该文件对应的界面中各个布局和控件的位置和大小,然后再来设置各个布局和控件的其他 ...
- Android基础教程(二)之五大布局对象---FrameLayout,LinearLayout ,AbsoluteLayout,RelativeLayout,TableLayout.
大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的),LinearLayout (线性布局),AbsoluteLayout(绝对 ...
- Android五大布局——LinearLayout、RelativeLayout
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLay ...
- Android 的LinearLayout (线性布局)和RelativeLayout (相对布局)介绍
LinearLayout (线性布局) 1. 在design页面下->component tree->ConstrainLayout右键->Convert view...-> ...
- 【Android】7.5 RelativeLayout(相对布局)
分类:C#.Android.VS2015: 创建日期:2016-02-11 一.简介 RelativeLayout是一种相对布局,容器中子元素的位置是相对于其前一个元素或者其他元素的位置来计算的,或者 ...
最新文章
- 盘点 Github 上的高仿 app 项目
- 一文带你领略并发编程的内功心法
- 训练生成对抗网络的过程中,训练gan的地方为什么这里没有detach,怎么保证训练生成器的时候不会改变判别器
- android drawable 比例,Android中的Drawable基础与自定义Drawable
- Java 获取昨天、当前、明天的时间
- Udp---模拟实现客户端与服务器通信
- nginx配置文件简单理解(自用)
- LeetCode:3Sum_15
- java.lang.UnsupportedOperationException: This parser does not support specification “null“ version “
- excel 平滑滚动_我的Excel书——Excel职场应用实战精粹
- [2018.07.31 T2] 第二题
- clion连接mysql,使用 CLion 调试 mysql 源码
- 工商银行在线支付接口
- 个人知识管理系统思源笔记
- 2020年四季度混合型基金数据分析
- speedoffice表格中如何隐藏网络线
- win10安装Mysql8的两种方式(安装/卸载-图文教程)
- 生活中的法则II(转载)
- 这两个能这样搭配?海关数据+决策人挖掘,用过的外贸人都说好!
- 用docker-compose来搭建Hadoop(一)——创建三个ubu