今天学习android界面设计的layout,记下过程,相当于是做笔记了。
首先是Framelayout:
Framelayout,字面意思上理解是“框架布局”。特点是 每个布局框架里头的元素都是在屏幕的左上角开始摆放,如果不止一个控件的话就相互重叠,后面的将前面的对象元素给覆盖掉。如果后面的对象是透明的话就可以看见前面的对象,不然后面的对象就会把前面的对象给覆盖。下面是例子:main.xml文件
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="hello ,this is a Button!" /> <Button android:layout_width="125sp" android:layout_height="wrap_content" android:text="this is the second button" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="#00aa00" /> </FrameLayout>
然后是Linearlayout:即相对布局。下面是main.xml文件根据xml布局文件就可以看出其用法。<!-- -->是xml文件的注释形式,这个比较重要,刚开始的时候还以为和.java文件里头的注释是一样的,闹了不少笑话。用//注释老是编译不过!悲剧了
<?xml version="1.0" encoding="utf-8"?> <!-- <LinearLayout> 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- android:orientation="vertical" 表示竖直方式对齐 android:orientation="horizontal"表示水平方式对齐 android:layout_width="fill_parent"定义当前视图在屏幕上 可以消费的宽度,fill_parent即填充整个屏幕。 android:layout_height="wrap_content":随着文字栏位的不同 而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思 layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。 所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight 值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布 局的layout_weight值中所占的比率而定。 举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。 该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。 如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分 在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个 文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2, 则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要 度越高)。 --> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="red" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="green" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="blue" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="yellow" android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2"> <TextView android:text="row one" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="row two" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="row three" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="row four" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> </LinearLayout>
我是初学,在网上看到有大虾说在android开发中这个linearlayout是用得比较多的,拭目以待吧,看起来是要灵活一些哦。
下面是Relativelayout:相对布局。xml文件如下面所示。
main.xml文件代码:
<?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" > <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:" android:textSize="15sp" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_below="@id/label" android:layout_marginBottom="10dp" android:text="在此输入文本..." /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" android:layout_alignParentRight="true" android:layout_below="@id/entry" android:layout_marginLeft="5dp" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> </RelativeLayout>
在相对布局中用得比较多的就我感觉就是相对屏幕位置左边呀,或者右边,顶部,等的位置。先记下来,以后在一步一步的研究,感觉这个在应用中还是蛮灵活的!
下面一个就是TableLayout,话不多说。现将xml文件和运行结果贴出来再说。
下面是xml文件:
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="0,1,2" > <TableRow> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, i am button1" android:layout_column="0" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, i am button2" android:layout_column="1" /> </TableRow> <TableRow> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, i am button3" android:layout_column="1" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, i am button4" android:layout_column="2" /> </TableRow> <TableRow> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, i am button5" android:layout_column="2" /> </TableRow> </TableLayout>
根据运行结果可以看出,每一个TableRow中的Button都是按照水平方向排列的,并且每个Button的位置可以用android:layout_column="2"来定义,其中参数表示位置,并且从0开始,如果是0就从屏幕的最左边开始排列,并且以此向后面排列。
最后就是AbsoluteLayout:绝对布局。此种布局形式是采用坐标的形式来排列activity中的对象。下面是xml文件:
main.xml:<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:text="input your name..." android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:layout_x="370px" android:layout_y="65px" android:layout_width="100px" android:layout_height="wrap_content" android:text="OK" /> </AbsoluteLayout>
运行结果截图:
以上就是android界面设计的5大布局哦!android学习必经之路!呵呵