设为首页 加入收藏

TOP

Android开发:Layout常用技巧(一)
2014-11-24 11:36:39 来源: 作者: 【 】 浏览:0
Tags: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 的。


< 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"


andro

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发:GridView常用技巧 下一篇Android开发:Theme常用技巧

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)