Android开发:Layout常用技巧
Android开发:Layout常用技巧
我们经常用到的控件都是 View 的派生类,他们通常都是可见的。 ViewGroup 也是 View 的派生类,但 ViewGroup 通常是不可见的。
ViewGroup 的主要作用:
+ 作为 Layout 。比如 LinearLayout 、 RelativeLayout 、 FrameLayout 和 TableLayout
+ 作为 View 的容器。比如 Gallery 、 GridView 、 ImageSwitcher 、 ScrollView 、 TabHost 和 ListView
其实 Layout 也可以被认为是 View 的一种容器。
本文仅讨论 ViewGroup 作为 Layout 的常用技巧。
1. LinearLayout
LinearLayout 顾名思义就是线性布局。其子 View 对象要么以 “ 行 ” 排列,要么以 “ 列 ” 排列,这取决于其 orientation 属性是 horizontal 还是 vertical 的。
创建一个 Android Project 项目。然后在创建一个 linearlayout.xml ,使其内容如下:
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" >
< Button
android:id = "@+id/linearbutton01"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = " 按钮 1"
/>
< Button
android:id = "@+id/linearbutton02"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = " 按钮 2"
/>
< Button
android:id = "@+id/linearbutton01"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = " 按钮 3"
/>
< Button
android:id = "@+id/linearbutton01"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = " 按钮 4"
/>
</ LinearLayout >
将 Activity 对应的代码中的 setContentView 的参数,改为: R.layout.linearlayout ,运行后得到的结果如下:
如果将 linearlayout.xml 中的 orientation 属性值改为 ”horizontal” ,那么运行后的结果如下:
2. RelativeLayout
RelativeLayout 可以根据子 View 对象彼此之间的位置关系来显示子 View 对象。比如通过 ”above” 、 ”below” 、 ”to the left of” 、 ”to the right of” 其他的子 View 对象来定位。
创建一个布局文件 relativelayout.xml ,使其内容如下:
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/relativelayout"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
< Button
android:id = "@+id/buttonCenter"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Center"
android:layout_centerInParent = "true"
/>
< ImageView
android:id = "@+id/ImageView01"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_above = "@id/buttonCenter"
android:layout_centerHorizontal = "true"
android:src = "@drawable/icon"
/>
< TextView
android:id = "@+id/textview01"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "@id/buttonCenter"
android:textSize = "20px"
android:text = "Android1"
/>
< TextView
android:id = "@+id/textview02"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "@id/buttonCenter"
android:layout_centerVertical = "true"
android:textSize = "20px"
android:text = "Android2"
/>
< TextView
android:id = "@+id/textview03"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@+id/textview01"
android:textSize = "20px"
android:text = "Android3"
/>
</ RelativeLayout >
将 Activity 对应的代码中的 setContentView 的参数,改为: R.layout.framelayout ,运行后得到的结果如下:
|
评论暂时关闭