博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android布局
阅读量:3983 次
发布时间:2019-05-24

本文共 2946 字,大约阅读时间需要 9 分钟。

一,LinearLayout(线性布局)中的特有属性,即控件(例如textview或imageview等)在L布局中才有的属性:

1,排列方式:

orientation=”horizontal横向排列(即控件横着排,一个控件占一列)/vertical纵向排列(即控件竖着排,一个控件占一行)”

2,子控件在L布局中的对齐方式:

(1)只在横排L布局中生效的:

layout_gravity="top"  控件在L布局的顶部

layout_gravity="bottom"  控件在L布局的底部

layout_gravity=”center_horizon”  控件在L布局的的横向方向上居中

(2)只是竖排L布局中生效的:

layout_gravity="left" 控件在L布局的左侧

layout_gravity="right"  控件在L布局的右侧

layout_gravity=”center_vertical”  控件在L布局的的纵向方向上居中

(3)横竖都生效的:

layout_gravity="center" ,在横排中相当于center_horizon,竖排中相当于center_vertical

3,权重:

Layout_weight =“1” 即布局中剩下的空间都属于该控件,如果有另一个控件也有个权重1的属性,则剩下的空间,两个权重1的控件平分

 

二,RelativeLayout(相对布局)中的特有属性,即控件(例如textview或imageview等)在R布局中才有的属性:

1,子控件在父布局中的对齐方式:

Layout_alignParentRight = “true”  控件在R布局的右侧

alignParentLeft 左侧,alignParentTop 顶部,alignParentBottom 底部

Layout_centerHorizon = “true” 控件在R布局的横向方向上居中

Layout_centerVertical= “true” 控件在R布局的纵向方向上居中

Layout_centerInParent = “true” 控件在R布局的正中

2,控件与控件之间的对齐方式:

Layout_alignLeft = “@+id/A” 与A控件左端对齐,right、top、bottom同理

Layout_above =  “@+id/A” 在A控件的上面,below同理

Layout_toLeftOf =  “@+id/A” 在A控件的左边,toRightOf同理

 

三,通用属性,即控件(例如textview或imageview等)在L、R布局中都有的属性:

1,maigin类:

Layout_margin = “10dp” 与四周的控件间距10dp

Layout_marginTop = “10dp” 当前控件的顶部与其他控件间距10dp,marginRight、marginLeft、marginBottom同理

2,padding类:

padding = “10dp” 内容与其所在的控件的间距10dp

paddingLeft = “10dp” 左间距10dp,paddingRight、paddingTop、paddingBottom同理

3,gravity类

gravity = “bottom” 内容在其所在的控件的底部

gravity = “bottom|left” 内容在其所在的控件的左下角

gravity = “center_horizon” 内容在其所在的控件的横向方向上居中

gravity = “center_vertical” 内容在其所在的控件的纵向方向上居中

谷歌官方支持按照百分比来设置布局的宽高

支持和,分别继承自和

均支持以下属性:

  • layout_widthPercent
  • layout_heightPercent
  • layout_marginPercent
  • layout_marginLeftPercent
  • layout_marginTopPercent
  • layout_marginRightPercent
  • layout_marginBottomPercent
  • layout_marginStartPercent
  • layout_marginEndPercent
效果图:

如何使用:

添加支持库到你的工程(Studio)

[java]   
  1. dependencies {  
  2.     compile 'com.android.support:percent:22.2.0'  
  3. }  

布局文件

PercentRelativeLayout

[html]   
  1. <android.support.percent.PercentRelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <View  
  8.         android:id="@+id/top_left"  
  9.         android:layout_width="0dp"  
  10.         android:layout_height="0dp"  
  11.         android:layout_alignParentTop="true"  
  12.         android:background="#ff44aacc"  
  13.         app:layout_heightPercent="20%"  
  14.         app:layout_widthPercent="70%" />  
  15.   
  16.     <View  
  17.         android:id="@+id/top_right"  
  18.         android:layout_width="0dp"  
  19.         android:layout_height="0dp"  
  20.         android:layout_alignParentTop="true"  
  21.         android:layout_toRightOf="@+id/top_left"  
  22.         android:background="#ffe40000"  
  23.         app:layout_heightPercent="20%"  
  24.         app:layout_widthPercent="30%" />  
  25.   
  26.   
  27.     <View  
  28.         android:id="@+id/bottom"  
  29.         android:layout_width="match_parent"  
  30.         android:layout_height="0dp"  
  31.         android:layout_below="@+id/top_left"  
  32.         android:background="#ff00ff22"  
  33.         app:layout_heightPercent="80%" />  
  34. </android.support.percent.PercentRelativeLayout>  

非常简单易用!

GitHub示例:

官方开发文档:

你可能感兴趣的文章
iwconfig tools
查看>>
Qt wifi helper script
查看>>
An example of how to streaming audio over network using Qt
查看>>
How to Properly Write Received UDP Audio Data to ALSA with C++
查看>>
About a full duplex ALSA application
查看>>
Creating a Ring Buffer
查看>>
capture-send-play
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
ALSA howto
查看>>
gpio matrix keypad linux driver
查看>>
Qt5 mouseless 测试
查看>>
qt5 mouseless 多种部件切换带焦点高亮显示
查看>>
udev和mdev hotplug事件
查看>>
And that’s what the lowest level of the Libevent API does for you
查看>>
webbench源码分析
查看>>
httpd源码分析
查看>>
192.168.1.1/24 什么含义
查看>>