Android布局

来源:互联网 发布:淘宝女装店铺推荐 编辑:程序博客网 时间:2024/06/06 00:02

一,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” 内容在其所在的控件的纵向方向上居中



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


支持PercentRelativeLayout和PercentFrameLayout,分别继承自RelativeLayout和FrameLayout

均支持以下属性:

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





如何使用:

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

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

布局文件

PercentRelativeLayout

[html] view plain copy
  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示例:

https://github.com/JulienGenoud/android-percent-support-lib-sample

官方开发文档:

https://juliengenoud.github.io/android-percent-support-lib-sample/



0 0
原创粉丝点击