本博文为子墨原创,转载请注明出处! http://blog.csdn.net/zimo2013/article/details/11840079

1.构建xml布局文件

使用android提供的xml布局,可以快速构建UI界面,每个xml文件必须包含一个根元素,该xml文件位于res/layout/目录中。例如:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello, I am a TextView" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello, I am a Button" /></LinearLayout>

2.加载xml布局文件

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//加载xml布局文件应该位于首行setContentView(R.layout.activity_main);}

3.属性ID

(1).xml中添加ID属性

android:id="@+id/button"

(2).xml中引用ID

android:id="@id/button"

(3).得到控件对象

Button myButton = (Button) findViewById(R.id.button);

4.布局的宽与高属性

所有视图都应该指定layout_width与layout_height属性,对该属性的描述主要有2个值

wrap_content根据控件内容的大小来定义当前控件整体大小

match_parent填满父类允许填满的空间位置,在 API Level 8之前使用fill_parent

一般在指定宽高时,推荐使用dp,而不是pixels。因为在不同的尺寸屏幕手机上,dp有更好的展示效果.

在LinearLayout布局中,假设定义了android:orientation="horizontal",且使用了android:layout_weight来表示子类控件的权重,此时子类控件推荐使用layout_width="0dp",子类控件将根据权重大小等比宽度;如果子类控件使用了layout_width="match_parent",将根据权重等比缩小。同样竖直方位也是如此!

5.layout_gravity与gravity

layout_gravity即设置当前视图相对于父类的位置摆放,gravity即控制当前视图的子类相对于父类的位置摆放。但有时layout_gravity不会起作用,原因很简单,就是忽略了父类的方位布局!

比如父类布局设置了android:orientation="vertical",而其子类有个button按钮,设置了android:layout_gravity="center_vertical",不会有任何效果,因为button遵循竖直排布,但只有水平方向的移动才会有效果!

因此,父类布局为vertical时,在设置layout_gravity时,只能控制水平方向的移动;在父类布局为horizontal时,在设置layout_gravity时,只能控制竖直方向的移动!而layout_gravity="center"即为可能有效果的那个方位居中!

6.margin与padding

margin主要控制当前视图相对于父类视图相间的位置距离大小

padding,比如一个Button按钮,text="Bt";如果指定了android:paddingLeft="30dp",意为Bt距离当前控件的左边为30dp大小

7.常见布局

(1).LinearLayout

LinearLayout为一个视图组,所有子类按照一单方向排列,必须指定android:orientation属性,该值为horizontal或者vertical,当使用子类控件使用权重layout_weight时,宽或者高推荐使用0dp

(2).RelativeLayout

RelativeLayout一个布局相对于另一个视图的位置,在上下左右或者相对于父类等等

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="16dp"    android:paddingRight="16dp" >    <EditText        android:id="@+id/name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="@string/reminder" />    <Spinner        android:id="@+id/dates"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@id/name"        android:layout_toLeftOf="@+id/times" />    <Spinner        android:id="@id/times"        android:layout_width="96dp"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/name" />    <Button        android:layout_width="96dp"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/times"        android:text="@string/done" /></RelativeLayout>

更多相关文章

  1. Android(安卓)ListView展示不同的布局
  2. Android(安卓)模仿renren的左右划动菜单栏
  3. RemoteViews用法一:widget简单用法
  4. [置顶] Android中ScrollView用法介绍
  5. ViewPager占用满屏问题解决方法
  6. MonkeyRunner进坑——errors importing other modules
  7. Android(安卓)程式开发:(十一)选择控件 —— 11.1 TimePicker
  8. [置顶] Android(安卓)SimpleAdapter,SimpleCursorAdapter,ArrayAda
  9. Android源码分享-自动换行LinearLayout

随机推荐

  1. Android bitmaps and out of memory erro
  2. Android权限设置android.permission完整
  3. Android 中文设置成粗体的方法
  4. Android零基础入门第25节:简单且最常用的L
  5. [Unity3D]生成Android(安卓)apk
  6. Android(安卓)OpenGL(二) 学习《Android(
  7. Android 打开设置的各个页面
  8. Android(安卓)AutoLayout全新的适配方式
  9. android studio windows 报错 ninja: bui
  10. Android(安卓)任务栈