android 的5种布局




常用五种布局方式,分别是:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。 
一、FrameLayout:所有东西依次都放在左上角,会重叠,这个布局比较简单,也只能放一点比较简单的东西。二、LinearLayout:线性布局,每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。三、AbsoluteLayout:绝对布局用X,Y坐标来指定元素的位置,这种布局方式也比较简单,但是在屏幕旋转时,往往会出问题,而且多个元素的时候,计算比较麻烦。四、RelativeLayout:相对布局可以理解为某一个元素为参照物,来定位的布局方式。主要属性有:相对于某一个元素android:layout_below、      android:layout_toLeftOf相对于父元素的地方android:layout_alignParentLeft、android:layout_alignParentRigh;五、TableLayout:表格布局,每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素。每一个布局都有自己适合的方式,这五个布局元素可以相互嵌套应用,做出美观的界面。









5大布局方式有以下几种:

  • 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
  • 帧布局(FrameLayout):组件从屏幕左上方布局组件。
  • 表格布局(TableLayout):按照行列方式布局组件。
  • 相对布局(RelativeLayout):相对其它组件的布局方式。
  •  绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
 
1. 线性布局

线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。

常用的属性:

android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小

第一个实例

①效果图:

 

②核心代码如下:

main.xml

  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     >       
  7.     <LinearLayout 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:orientation="vertical" 
  11.         > 
  12.         <EditText 
  13.             android:layout_width="fill_parent" 
  14.             android:layout_height="wrap_content" 
  15.             /> 
  16.     </LinearLayout> 
  17.     <LinearLayout 
  18.         android:layout_width="fill_parent" 
  19.         android:layout_height="wrap_content" 
  20.         android:orientation="horizontal" 
  21.         android:gravity="right" 
  22.         > 
  23.     <!-- android:gravity="right"表示Button组件向右对齐 --> 
  24.         <Button 
  25.             android:layout_height="wrap_content" 
  26.             android:layout_width="wrap_content" 
  27.             android:text="确定" 
  28.             /> 
  29.         <Button 
  30.             android:layout_height="wrap_content" 
  31.             android:layout_width="wrap_content" 
  32.             android:text="取消" 
  33.             />    
  34.      </LinearLayout> 
  35. </LinearLayout> 

第二个实例

①效果图:

 

 

②核心代码:

mian.xml
 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.  
  6.     <LinearLayout 
  7.     android:orientation="horizontal" 
  8.     android:layout_width="fill_parent" 
  9.     android:layout_height="fill_parent" 
  10.     android:layout_weight="1"> 
  11.       
  12.     <TextView 
  13.         android:text="red" 
  14.         android:gravity="center_horizontal" 
  15.         android:background="#aa0000" 
  16.         android:layout_width="wrap_content" 
  17.         android:layout_height="fill_parent" 
  18.         android:layout_weight="1" 
  19.         /> 
  20.      <!--android:gravity="center_horizontal"水平居中 -->   
  21.      <!--layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。  
  22.          线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。  
  23.         例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,  
  24.         那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;  
  25.         对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,  
  26.         再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度--> 
  27.     <TextView 
  28.         android:text="Teal" 
  29.         android:gravity="center_horizontal" 
  30.         android:background="#008080" 
  31.         android:layout_width="wrap_content" 
  32.         android:layout_height="fill_parent" 
  33.         android:layout_weight="1"/> 
  34.       
  35.     <TextView 
  36.         android:text="blue" 
  37.         android:gravity="center_horizontal" 
  38.         android:background="#0000aa" 
  39.         android:layout_width="wrap_content" 
  40.         android:layout_height="fill_parent" 
  41.         android:layout_weight="1" 
  42.         /> 
  43.       
  44.     <TextView 
  45.         android:text="orange" 
  46.         android:gravity="center_horizontal" 
  47.         android:background="#FFA500" 
  48.         android:layout_width="wrap_content" 
  49.         android:layout_height="fill_parent" 
  50.         android:layout_weight="1" 
  51.         /> 
  52.           
  53.     </LinearLayout>   
  54.     <LinearLayout 
  55.     android:orientation="vertical" 
  56.     android:layout_width="fill_parent" 
  57.     android:layout_height="fill_parent" 
  58.     android:layout_weight="1"> 
  59.       
  60.     <TextView 
  61.         android:text="row one" 
  62.         android:textSize="15pt" 
  63.         android:background="#aa0000" 
  64.         android:layout_width="fill_parent" 
  65.         android:layout_height="wrap_content" 
  66.         android:layout_weight="1" 
  67.         /> 
  68.     <!--  -->   
  69.     <TextView 
  70.         android:text="row two" 
  71.         android:textSize="15pt" 
  72.         android:background="#DDA0DD" 
  73.         android:layout_width="fill_parent" 
  74.         android:layout_height="wrap_content" 
  75.         android:layout_weight="1" 
  76.         /> 
  77.       
  78.     <TextView 
  79.         android:text="row three" 
  80.         android:textSize="15pt" 
  81.         android:background="#008080" 
  82.         android:layout_width="fill_parent" 
  83.         android:layout_height="wrap_content" 
  84.         android:layout_weight="1" 
  85.         />    
  86.     <TextView 
  87.         android:text="row four" 
  88.         android:textSize="15pt" 
  89.         android:background="#FFA500" 
  90.         android:layout_width="fill_parent" 
  91.         android:layout_height="wrap_content" 
  92.         android:layout_weight="1" 
  93.         />       
  94.     </LinearLayout>   
  95. </LinearLayout> 
2.  帧布局
帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
 
 简单的例子
①效果图:
 
② 核心代码:
main.xml
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     > 
  6.     <TextView    
  7.         android:layout_width="300dp"   
  8.         android:layout_height="300dp"   
  9.         android:background="#00BFFF"          
  10.         /> 
  11.     <TextView    
  12.         android:layout_width="260dp"   
  13.         android:layout_height="260dp"   
  14.         android:background="#FFC0CB"          
  15.         /> 
  16.     <TextView    
  17.         android:layout_width="220dp"   
  18.         android:layout_height="220dp"   
  19.         android:background="#0000FF"          
  20.         /> 
  21. </FrameLayout> 
 
3. 表格布局
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
 
简单的列子:
①效果图:
 
② 核心代码:
 main.xml
 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     > 
  6.     <TableRow> 
  7.         <Button 
  8.             android:text="Button1" 
  9.             /> 
  10.         <Button 
  11.             android:text="Button2" 
  12.             /> 
  13.         <Button 
  14.             android:text="Button3" 
  15.             /> 
  16.     </TableRow> 
  17.     <TableRow> 
  18.         <Button 
  19.             android:text="Button4" 
  20.             /> 
  21.         <Button 
  22.             android:layout_span="2" 
  23.             android:text="Button5" 
  24.             /> 
  25.     </TableRow> 
  26.       
  27. </TableLayout> 
 
4. 相对布局
相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
相对布局常用属性请参考我博客的: http://liangruijun.blog.51cto.com/3061169/631816
 
简单的例子
①效果图:
 
② 核心代码:
main.xml
 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="wrap_content" 
  5.     android:padding="10px" 
  6.     > 
  7.     <TextView    
  8.         android:id="@+id/tev1" 
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content"   
  11.         android:layout_marginBottom="30dp" 
  12.         android:text="Please Type Here:" 
  13.         /> 
  14.     <EditText 
  15.         android:id="@+id/tx1" 
  16.         android:layout_width="match_parent" 
  17.         android:layout_height="wrap_content" 
  18.         android:layout_below="@id/tev1" 
  19.         /> 
  20.     <Button 
  21.         android:id="@+id/btn1" 
  22.         android:layout_height="wrap_content" 
  23.         android:layout_width="wrap_content" 
  24.         android:layout_below="@id/tx1" 
  25.         android:layout_alignParentRight="true" 
  26.         android:text="确定" 
  27.         /> 
  28.     <Button 
  29.         android:id="@+id/btn2" 
  30.         android:layout_height="wrap_content" 
  31.         android:layout_width="wrap_content" 
  32.         android:layout_below="@id/tx1" 
  33.         android:layout_toLeftOf="@id/btn1" 
  34.         android:layout_marginRight="30dp" 
  35.         android:text="取消" 
  36.         /> 
  37. </RelativeLayout> 
5.  绝对布局
 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。















 
ViewGroupWidth/Height , Padding/margin
FrameLayoutgravity
LinearLayout
Orientation/horizontal/vertical
weight
RelativeLayout
alignBaseline/(Left/Top/Right/Bottom) Start/End
alignParent
center  Horizontal/Vertical/InParent
below/above/toLeftOf/toRightOf/toStartOf/toEndOf


LinearLayout代码设置weight
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
linearLayout.setLayoutParams(lp);


Android布局优化 (非常好的一篇文章)
http://www.infoq.com/cn/articles/android-optimise-layout

selector几种状态
state_pressed
state_focused
state_selected


TextView常用属性
android:ellipsize="none"
android:singleLine="true"
android:textColor=""
android:textSize=""





一、RelativeLayout布局属性 与 include、merge简单总结


// 当前视图顶部,底部,左侧,右侧与其他视图间填充区域

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_marginTop  
  2. android:layout_marginBottom  
  3. android:layout_marginLeft  
  4. android:layout_marginRight  

java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3. // 单位是px  
  4. layoutParams.topMargin = 66;  
  5. layoutParams.bottomMargin = 66;  
  6. layoutParams.leftMargin = 66;  
  7. layoutParams.rightMargin = 66;  





// 当前视图内顶部,底部,左侧,右侧填充区域 (非RelativeLayout属性)
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:paddingTop  
  2. android:paddingBottom  
  3. android:paddingLeft  
  4. android:paddingRight  





// 在指定ID视图上面,下面,左侧,右侧

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_above  
  2. android:layout_below  
  3. android:layout_toLeftOf  
  4. android:layout_toRightOf  

Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.ABOVE, R.id.viewId);  
  5. layoutParams.addRule(RelativeLayout.BELOW, R.id.viewId);  
  6. layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.viewId);  
  7. layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.viewId);  




 
// 与制定ID视图baseLine,顶部,底部,左侧,右侧边缘对齐
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_alignBaseline  
  2. android:layout_alignTop  
  3. android:layout_alignBottom  
  4. android:layout_alignLeft  
  5. android:layout_alignRight  
Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, R.id.viewId);  
  5. layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.viewId);  
  6. layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.viewId);  
  7. layoutParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.viewId);  
  8. layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.viewId);  




// 与父视图顶部,底部,左侧,右侧边缘对齐
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_alignParentTop  
  2. android:layout_alignParentBottom  
  3. android:layout_alignParentLeft  
  4. android:layout_alignParentRight  

Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
  5. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  6. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  7. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  






// 横向居中,纵向居中,针对与父视图剧中
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_centerHorizontal  
  2. android:layout_centerVertical  
  3. android:layout_centerInParent  

Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
  5. layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);  
  6. layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);  



** Include 与 mege标签

参考资料:

Re-using Layouts with <include/>

Romain Guy   Android Layout Tricks #3: Optimize, Part 1


** ViewGroup相关配置

1. clipChildren

android:clipChildren setClipChildren(boolean)Defines whether a child is limited to draw inside of its bounds or not. 
定义是否限制子视图在它的范围内进行绘制。默认是true

2. clipToPadding
android:clipToPadding setClipToPadding(boolean)Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. 
定义ViewGroup是否将剪切它的绘制界面并排除padding区域。默认是true

参考资料:

ViewGroup API




* 配置文件属性与Java代码方法对应
android:gravity
android:layout_gravity

android:layout_gravity="right"
lp.gravity = Gravity.RIGHT; 
button.setGravity(Gravity.CENTER); 



* View属性,子视图与父控件perssed 等状态保持一致
xml配置:
android:duplicateParentState
代码:
setDuplicateParentStateEnabled

* ViewGroup属性,父视图与子视图pressed状态保持一致
xml配置:
android:addStatesFromChildren
代码:
setAddStatesFromChildren


设置EditText光标位置

http://www.cnblogs.com/nanhuchongzi/archive/2012/04/17/2444451.html



** margin与padding的区别?

一个属于视图内,一个属于试图外
margin之后,如果视图隐藏,margin不会隐藏
因为margin是视图外的部分
padding会算如总宽吗?
会算入总宽


** 动态修改宽高
view.getLayoutParams().width = widthValue;



Android进阶练习-改善布局性能
http://blog.csdn.net/tu_bingbing/article/details/8654990



**TextView 显示两行,其余用....替换
android:maxLines="2"
android:ellipsize="end"


values/ids.xml
<resources>
  <item type="id" name="idname" />
</resources>
view.setId(R.id. idname);


*** 

布局XML Schema
有哪些属性,是在XML Schema定义,可以在http://www.w3school.com.cn/schema/schema_intro.asp了解Schema更多知识。
Android 布局XML Schema在什么地方? 查询补漏

布局在Java中的对应关系 TypeArray


原文地址:http://blog.csdn.net/love_world_/article/details/8426935



2013-12-29  添加gravity、 layout_gravity、duplicateParentState

2014-04-10 添加布局XML Schema









 
ViewGroupWidth/Height , Padding/margin
FrameLayoutgravity
LinearLayout
Orientation/horizontal/vertical
weight
RelativeLayout
alignBaseline/(Left/Top/Right/Bottom) Start/End
alignParent
center  Horizontal/Vertical/InParent
below/above/toLeftOf/toRightOf/toStartOf/toEndOf


LinearLayout代码设置weight
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
linearLayout.setLayoutParams(lp);


Android布局优化 (非常好的一篇文章)
http://www.infoq.com/cn/articles/android-optimise-layout

selector几种状态
state_pressed
state_focused
state_selected


TextView常用属性
android:ellipsize="none"
android:singleLine="true"
android:textColor=""
android:textSize=""





一、RelativeLayout布局属性 与 include、merge简单总结


// 当前视图顶部,底部,左侧,右侧与其他视图间填充区域

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_marginTop  
  2. android:layout_marginBottom  
  3. android:layout_marginLeft  
  4. android:layout_marginRight  

java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3. // 单位是px  
  4. layoutParams.topMargin = 66;  
  5. layoutParams.bottomMargin = 66;  
  6. layoutParams.leftMargin = 66;  
  7. layoutParams.rightMargin = 66;  





// 当前视图内顶部,底部,左侧,右侧填充区域 (非RelativeLayout属性)
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:paddingTop  
  2. android:paddingBottom  
  3. android:paddingLeft  
  4. android:paddingRight  





// 在指定ID视图上面,下面,左侧,右侧

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_above  
  2. android:layout_below  
  3. android:layout_toLeftOf  
  4. android:layout_toRightOf  

Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.ABOVE, R.id.viewId);  
  5. layoutParams.addRule(RelativeLayout.BELOW, R.id.viewId);  
  6. layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.viewId);  
  7. layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.viewId);  




 
// 与制定ID视图baseLine,顶部,底部,左侧,右侧边缘对齐
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_alignBaseline  
  2. android:layout_alignTop  
  3. android:layout_alignBottom  
  4. android:layout_alignLeft  
  5. android:layout_alignRight  
Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, R.id.viewId);  
  5. layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.viewId);  
  6. layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.viewId);  
  7. layoutParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.viewId);  
  8. layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.viewId);  




// 与父视图顶部,底部,左侧,右侧边缘对齐
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_alignParentTop  
  2. android:layout_alignParentBottom  
  3. android:layout_alignParentLeft  
  4. android:layout_alignParentRight  

Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
  5. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  6. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  7. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  






// 横向居中,纵向居中,针对与父视图剧中
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:layout_centerHorizontal  
  2. android:layout_centerVertical  
  3. android:layout_centerInParent  

Java代码设置

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  
  2.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3.   
  4. layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
  5. layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);  
  6. layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);  



** Include 与 mege标签

参考资料:

Re-using Layouts with <include/>

Romain Guy   Android Layout Tricks #3: Optimize, Part 1


** ViewGroup相关配置

1. clipChildren

android:clipChildren setClipChildren(boolean)Defines whether a child is limited to draw inside of its bounds or not. 
定义是否限制子视图在它的范围内进行绘制。默认是true

2. clipToPadding
android:clipToPadding setClipToPadding(boolean)Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. 
定义ViewGroup是否将剪切它的绘制界面并排除padding区域。默认是true

参考资料:

ViewGroup API




* 配置文件属性与Java代码方法对应
android:gravity
android:layout_gravity

android:layout_gravity="right"
lp.gravity = Gravity.RIGHT; 
button.setGravity(Gravity.CENTER); 



* View属性,子视图与父控件perssed 等状态保持一致
xml配置:
android:duplicateParentState
代码:
setDuplicateParentStateEnabled

* ViewGroup属性,父视图与子视图pressed状态保持一致
xml配置:
android:addStatesFromChildren
代码:
setAddStatesFromChildren


设置EditText光标位置

http://www.cnblogs.com/nanhuchongzi/archive/2012/04/17/2444451.html



** margin与padding的区别?

一个属于视图内,一个属于试图外
margin之后,如果视图隐藏,margin不会隐藏
因为margin是视图外的部分
padding会算如总宽吗?
会算入总宽


** 动态修改宽高
view.getLayoutParams().width = widthValue;



Android进阶练习-改善布局性能
http://blog.csdn.net/tu_bingbing/article/details/8654990



**TextView 显示两行,其余用....替换
android:maxLines="2"
android:ellipsize="end"


values/ids.xml
<resources>
  <item type="id" name="idname" />
</resources>
view.setId(R.id. idname);


*** 

布局XML Schema
有哪些属性,是在XML Schema定义,可以在http://www.w3school.com.cn/schema/schema_intro.asp了解Schema更多知识。
Android 布局XML Schema在什么地方? 查询补漏

布局在Java中的对应关系 TypeArray


原文地址:http://blog.csdn.net/love_world_/article/details/8426935



2013-12-29  添加gravity、 layout_gravity、duplicateParentState

2014-04-10 添加布局XML Schema


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值