Android中xml布局文件

LinearLayoutRelativeLayout

共有属性
java代码中通过btn关联次控件
android:id="@+id/btn"

控件宽度:

android:layout_width="80px"    //"80dip"或"80dp"
android:layout_width =“wrap_content”
android:layout_width =“match_parent” 

控件高度:
android:layout_height="80px"    //"80dip"或"80dp"
android:layout_height =“wrap_content”
android:layout_height =“match_parent”

控件排布:
android:orientation="horizontal”//水平排布
android:orientation="vertical“//竖直排布

控件间距:
android:layout_marginLeft="5dip"      //距离左边
android:layout_marginRight="5dip"      //距离右边
android:layout_marginTop="5dip"      //距离上面
android:layout_marginRight="5dip"      //距离下面

控件显示位置:
android:gravity="center"    //left,right, top, bottom
android:gravity="center_horizontal"

android:layout_gravity是本元素对父元素的重力方向。
android:layout_gravity属性则设置控件本身相对于父控件的显示位置
android:gravity是本元素所有子元素的重力方向。

android:layout_gravity="center_vertical"
android:layout_gravity="left"
android:layout_gravity="left|bottom"

TextView中文本字体:
android:text="@String/text1"    //在string.xml中定义text1的值
android:textSize="20sp"
android:textColor=”#ff123456”
android:textStyle="bold"    //普通(normal), 斜体(italic),粗斜体(bold_italic)

定义控件是否可见:
android:visibility=”visible”     //可见
android:visibility=”invisible”  //不可见,但是在布局中占用的位置还在
android:visibility=”gone”   //不可见,完全从布局中消失

定义背景图片:
android:background="@drawable/img_bg"    //img_bg为drawable下的一张图片

seekbar控件背景图片及最大值
android:progressDrawable="@drawable/seekbar_img"  
android:thumb="@drawable/thumb"            
android:max = "60"

仅在RelativeLayout中有效:
在父亲布局的相对位置
android:layout_alignParentLeft="true"     //在布局左边
android:layout_alignParentRight="true"    //在布局右边
android:layout_alignParentTop="true"     //在布局上面
android:layout_alignParentBottom="true "  //在布局的下面

在某个控件的相对位置
android:layout_toRightOf="@id/button1"  //在控件button1的右边,不仅仅是紧靠着
android:layout_toLeftOf="@id/button1"   //在控件button2的左边,不仅仅是紧靠着
android:layout_below="@id/button1 "     //在控件button1下面,不仅仅是正下方
android:layout_above=“@id/button1”   //在控件button1下面,不仅仅是正下方

定义和某控件对奇
android:layout_alignTop=”@id/button1”  //和控件button1上对齐
android:layout_alignBottom=”@id/button1”  //和控件button1下对齐
android:layout_alignLeft=”@id/button1”  //和控件button1左对齐
android:layout_alignRight=”@id/button1”  //和控件button2右对齐

android:layout_centerHorizontal="true"        //水平居中
android:layout_centerVertical="true"
android:layout_centerInParent="true"

仅在LinearLayout中有效
设置控件在一排或一列中所占比例值
android:layout_weight="1"

时间: 2024-08-10 05:29:09

Android中xml布局文件的相关文章

Android中将xml布局文件转化为View树的过程分析(上)

有好几周没写东西了,一方面是因为前几个周末都有些事情,另外也是因为没能找到好的写作方向,或者说有些话题 值得分享.写作,可是自己积累还不够,没办法只好闷头继续研究了.这段时间一边在写代码,一边也在想Android中 究竟是如何将R.layout.xxx_view.xml这样的布局文件加载到Android系统的view层次结构中的(即我们常说的view树). 这期间一方面自己研究了下源码,另一方面也在网上搜索了下相关文章,发现了2篇很不错的同主题文章,推荐给大家: http://blog.csdn

Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析

在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever you want LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 2. you're in some View class, then jus

Android中将xml布局文件转换为View树的过程分析(上)

有好几周没写东西了,一方面是因为前几个周末都有些事情,另外也是因为没能找到好的写作方向,或者说有些话题 值得分享.写作,可是自己积累还不够,没办法只好闷头继续研究了.这段时间一边在写代码,一边也在想Android中 究竟是如何将R.layout.xxx_view.xml这样的布局文件加载到Android系统的view层次结构中的(即我们常说的view树). 这期间一方面自己研究了下源码,另一方面也在网上搜索了下相关文章,发现了2篇很不错的同主题文章,推荐给大家: http://blog.csdn

Android中五中布局文件的使用和介绍

Android的布局风格   布局应该从外往里写 1.LinearLayout(线性布局) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layo

Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(上

                                                                                                                                               本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 在之前一篇博文中<<Android中View绘制流程以及invalidate()等相关方法分析>>,简单的阐述

Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析

转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PARENT/FILL_PARENT属性的原理说明 xml布局文件解析成View树的流程分析. 希望对大家能有帮助.- - 分析版本基于Android 2.3 . 1.WRAP_CONTENT.MATCH_PARENT/FILL_PARENT 初入Android殿堂的同学们,对这三个属性一定又爱又恨.爱的是使

【转】Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(下)

转载请注明出处:http://blog.csdn.net/qinjuning 上篇文章<<Android中measure过程.WRAP_CONTENT详解以及xml布局文件解析流程浅析(上)>>中,我们 了解了View树的转换过程以及如何设置View的LayoutParams的.本文继续沿着既定轨迹继续未完成的job. 主要知识点如下:                 1.MeasureSpc类说明                 2.measure过程详解(揭秘其细节);   

[图文]为移植到Android平台上的Cocos2d-x项目添加xml布局文件

转载请标明出处:http://blog.csdn.net/vistatns/article/details/51316462 (将Cocos2d-x项目移植到Android上请前往:http://blog.csdn.net/vistatns/article/details/51316103) 1.添加布局文件main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:a

android studio从布局文件中提取style

写一个复杂的布局文件:当快写完时,发现已经快1000行代码啦,虽然有空格,但是布局也显得太庞大啦,无意间发现android studio从布局文件中提取style的方法,很是方便. 首先在布局文件中正常编写View的属性,然后点击右键打开菜单,依次选择Refactor -> Extract -> Style,如图所示: 其次,在弹出的提取style对话框中,选择所需要的属性,如图所示: 最后,为style命名,点击ok,然后就可以在style.xml文件中看到这个style了,如图所示: 就这