Android学习.1(线性布局和相对布局)
1. 线性布局(LinearLayout):在该标签下的所有子元素会根据orientation属性的值来决定是按行或者是按列来逐个显示。代码示例如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test" />
</LinearLayout>
就会产生这样的效果:
2. 另外还有相对布局(RelativeLayout),比较简单,这里不再赘述。
3. 在实际中RelativeLayout和LinearLayout一般搭配使用,例如将刚才的代码改变一下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:layout_alignTop="@id/button1"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/app_name" />
</RelativeLayout>
</LinearLayout>
就有了这样的效果:
4. 表格布局(TableLayout)与html中的表格布局类似,其中TableRow标签代表一个行,而TextView标签代表其中的一个元素。 表格布局和帧布局在初期用的较少,所以以后接触到了再说。
作者:michaelxi007 发表于2013-12-2 20:27:56 原文链接
阅读:18 评论:0 查看评论
浙公网安备 33010602011771号