- 浏览: 328763 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
crxiang:
写得很好啊,最近正好需要这个,谢谢分享了!
Android中ProgressDialog的简单示例 -
shangs2010:
分析的真仔细,多谢!
SharePreference类似于JavaEE中的session -
guochongcan:
fantaxy025025 写道兄弟,你这个不行的。
只能删 ...
MYSQL用一条SQL语句删除重复记录 -
fantaxy025025:
兄弟,你这个不行的。只能删除 重复个数是2的,如果重复个数大于 ...
MYSQL用一条SQL语句删除重复记录 -
wujiandong:
不错,多写点这方面的东西~~,加油~~,哥们~~
布局学习——妙用TabHost
前言
Android布局中RelativeLayout非常灵活,之前项目使用很少,一般的例子用得都比较呆板。在学习中发现其大量采用了该布局,这里"帮"他分享一下一些经典的用法:)
声明
欢迎转载,但请保留文章原始出处:)
JavaEye社区:http://www.iteye.com
农民伯伯: http://over140.cnblogs.com
系列
正文
一、效果图
格子布局效果:
(图一)
居中正在加载的效果:
二、实现代码
2.1 实现 图一 效果代码
android:background="@drawable/panel_bg" android:layout_width="fill_parent"
android:layout_height="100.0dip" android:layout_margin="10.0dip">
<TextView android:textSize="16.0sp" android:textColor="#ff7d899d"
android:gravity="center_vertical" android:id="@id/tvAddress"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip" android:layout_marginTop="10.0dip"
android:text="@string/userinfo_address"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView android:textSize="16.0sp" android:textColor="#ff373737"
android:id="@id/tvAddress_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="10.0dip"
android:layout_toRightOf="@id/tvAddress" android:layout_alignTop="@id/tvAddress" />
<View android:id="@id/vHDivider" android:background="@drawable/horizontal_separation_line_repeat"
android:layout_width="fill_parent" android:layout_height="1.0dip"
android:layout_centerVertical="true" />
<TextView android:textSize="16.0sp" android:textColor="#ff7d899d"
android:gravity="center_vertical" android:id="@id/tvAccount_info"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/account_info" android:layout_below="@id/vHDivider"
android:layout_alignLeft="@id/tvAddress"
android:layout_alignParentBottom="true" />
<TextView android:textSize="16.0sp" android:textColor="#ff373737"
android:gravity="center_vertical" android:id="@id/tvAccount_info_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip" android:layout_marginBottom="12.0dip"
android:singleLine="true" android:layout_toRightOf="@id/tvAccount_info"
android:layout_alignBottom="@id/tvAccount_info" />
</RelativeLayout>
<RelativeLayout android:background="@drawable/panel_bg"
android:layout_width="fill_parent" android:layout_height="130.0dip"
android:layout_margin="10.0dip">
<View android:id="@id/vVDivider1" android:background="@drawable/vertical_separation_line_repeat"
android:layout_width="1.0dip" android:layout_height="fill_parent"
android:layout_centerHorizontal="true" />
<View android:id="@id/vHDivider2" android:background="@drawable/horizontal_separation_line_repeat"
android:layout_width="fill_parent" android:layout_height="1.0dip"
android:layout_centerVertical="true" />
<RelativeLayout android:id="@id/llAttention"
android:background="@drawable/bg_panel_above_left"
android:clickable="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="@id/vVDivider1"
android:layout_above="@id/vHDivider2"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<TextView android:gravity="center" android:id="@id/tvAttention_count"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="10.0dip" android:text="0"
android:layout_centerHorizontal="true"
style="@style/userinfo_panel_textview_count" />
<TextView android:gravity="center" android:id="@id/tvAttention"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/attention" android:layout_below="@id/tvAttention_count"
android:layout_centerHorizontal="true"
style="@style/userinfo_panel_textview_title" />
</RelativeLayout>
<LinearLayout android:orientation="vertical"
android:id="@id/rlWeibo" android:background="@drawable/bg_panel_above_right"
android:clickable="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@id/vVDivider1"
android:layout_above="@id/vHDivider2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<TextView android:gravity="center" android:layout_gravity="center_horizontal"
android:id="@id/tvWeibo_count" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="10.0dip"
android:text="0" style="@style/userinfo_panel_textview_count" />
<TextView android:gravity="center" android:layout_gravity="center_horizontal"
android:id="@id/tvTopic" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/radio_button_topic"
style="@style/userinfo_panel_textview_title" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:id="@id/llFans" android:background="@drawable/bg_panel_below_left"
android:clickable="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="@id/vVDivider1"
android:layout_below="@id/vHDivider2"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true">
<TextView android:gravity="center" android:layout_gravity="center_horizontal"
android:id="@id/tvFans_count" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="10.0dip"
android:text="0" style="@style/userinfo_panel_textview_count" />
<TextView android:gravity="center" android:layout_gravity="center_horizontal"
android:id="@id/tvFans" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/fans"
style="@style/userinfo_panel_textview_title" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:id="@id/llTopic" android:background="@drawable/bg_panel_below_right"
android:clickable="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@id/vVDivider1"
android:layout_below="@id/vHDivider2"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true">
<TextView android:gravity="center" android:layout_gravity="center_horizontal"
android:id="@id/tvTopic_count" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="10.0dip"
android:text="0" style="@style/userinfo_panel_textview_count" />
<TextView android:gravity="center" android:layout_gravity="center_horizontal"
android:id="@id/tvTopic" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/his_topics"
style="@style/userinfo_panel_textview_title" />
</LinearLayout>
</RelativeLayout>
代码说明:
2.1.1 第一个RelativeLayout为图一上的实现代码。注意使用了一个View,也就是一条横线,令其居中布局;"地址:"的TextView通过layout_alignParentLeft和layout_alignParentTop令其在整个 RelativeLayout顶左顶上;"账号信息:"的TextView通过layout_below令其位于横线下方,layout_alignLeft令其与"地址:"的TextView左边对齐,然后用layout_alignParentBottom让其居于容器底部。
2.1.2 第二个RelativeLayout为图一下的实现代码。关键是vVDivider1和vVDivider2,与3.1.1类似,画出了一个十字架的布局,然后分别用居左、居上、居下、居右等方式实现了该布局效果。
2.2 实现 图二 效果代码
android:layout_width="fill_parent" android:background="#ffeff0f4"
android:visibility="gone" android:layout_height="fill_parent"
android:layout_weight="1.0">
<LinearLayout android:layout_centerInParent="true"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ProgressBar android:id="@+id/prb"
style="?android:attr/progressBarStyleSmallTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:text="@string/loadinfo"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
代码说明:
主要是layout_centerInParent属性的应用,令其居于RelativeLayout的中间。使用的时候领ListView先隐藏,然后让这个布局显示并填充,用完在设置Visible为GONE即可。
三、总结
熟练掌握以下重要属性,并灵活运用:
android:layout_centerVertical 水平居中布局
android:layout_centerHorizontal 垂直居中布局
android:layout_alignParentBottom 居于容器内底部
android:layout_alignParentLeft 居于容器内左边
android:layout_alignParentRight 居于容器内右边
android:layout_above 居于指定View的上方
android:layout_below 居于指定View的下方
android:layout_toRightOf 在指定View的右边
android:layout_toLeftOf 在指定View的左边
android:layout_alignTop 与指定View的Top一致
为了方便大家测试效果,文章末尾提供了资源下载,不足的资源请自行在APK中提取。
四、补充
相关文章
资源下载
发表评论
-
使用 SharedPreferences
2013-01-31 21:37 1232SharedPreferences是一种轻量级的数据存储 ... -
android:windowSoftInputMode属性详解
2012-11-01 07:36 843android:windowSoftInputMode ac ... -
自定义ListView中的分割线
2012-11-01 07:12 2302ListView中每个Item项之间都有分割线,设置andro ... -
Android中Bitmap和Drawable
2012-10-24 17:54 1003一、相关概念 1、Drawable就是一个可画的对象,其可能 ... -
在EditText/TextView中插入表情图片、样式、下划线等
2012-09-23 20:03 2704EditText: 通常用于显示 ... -
Android开发:在EditText中关闭软键盘
2012-09-23 14:41 18601、EditText有焦点(focusable为true) ... -
Android 字体和颜色
2012-06-23 18:34 990对于能够显示文字的控件(如TextView EditText ... -
解决Eclipse Indigo(3.7)中文字体偏小问题
2012-06-04 20:33 1115===========转============= ... -
Android中Cursor 的一些方法
2012-05-16 17:15 1484close() //关闭游标,释放资源 copyStrin ... -
ContentProvider分析
2012-05-15 22:24 1015红色部分较重要的 private static ... -
Java的log 的几种表示颜色
2012-05-09 16:12 0Log下面的日志颜色 E Error 红 ... -
android单元测试时,异常情况解决记录
2012-04-07 20:26 1559异常内容 java.lang.NoClassDefFo ... -
Android_SDK及ADT升级方法
2012-04-05 10:57 25445本文只讲如何 ... -
android 退出程序 对话框提醒
2012-03-17 20:23 0if (keyCode == KeyEvent.KEYCO ... -
DBA应该具有什么样的素质?——转
2012-03-17 20:18 0问题起源于在写一份材 ... -
抓住移动互联网发展最佳时机,开发最好的移动应用程序,为用户提供最佳的软件服务
2012-03-04 20:03 0抓住移动互联网发展最佳时机,开发最好的移动应用程序,为用户提供 ... -
Android WebView 浏览器
2012-01-01 09:38 4210WebView的使用很方便。在学习WebView之前,我 ... -
android ImageView的scaleType属性
2011-12-27 13:07 1200ImageView:Displays an arbitra ... -
程序实现线性布局相关
2011-12-26 15:42 1055程序实现线性布局相关1、 LinearLayout l ... -
Android中Toast的用法简介
2011-12-24 21:20 1428Toast是Android中用来显示显示信息的一种机制,和Di ...
相关推荐
新浪微博布局学习——活用RelativeLayout - 农民伯伯 - 博客园.mht453.32 KB Android 3.0 r1 API中文文档(113) ——SlidingDrawer .html116.92 KB Android 3.1 r1 中文API文档 (120) —— SearchView .html91.16 KB ...
1. **用户界面(UI)**:新浪微博客户端的UI设计遵循Material Design指南,使用了Android的布局组件如LinearLayout、RelativeLayout和ConstraintLayout来组织视图,同时运用RecyclerView进行列表展示,实现高效的数据...
- **开发背景与目的**:本项目旨在为Android初学者提供一个实践案例——开发一个新浪微博客户端,以加深对Android开发的理解,并通过实战掌握相关技能。 - **作者介绍**:文章作者自称“三无”(无基础、无经验、无...
《安卓Android源码——四次元新浪微博客户端项目》是一个针对Android平台的开源项目,它提供了对新浪微博客户端的实现。这个项目对于学习和研究Android应用开发,尤其是社交应用的开发具有很高的参考价值。以下是对...
4. **用户界面(UI)设计**:Android的布局文件(XML)定义了应用的视图结构,使用了LinearLayout、RelativeLayout、RecyclerView等组件来展示内容。同时,自定义View和Adapter可能被用来实现特定的界面效果。 5. *...
UI方面,Android的布局管理器(如LinearLayout、RelativeLayout、GridLayout)和自定义视图(View)的使用是关键。源码展示了如何构建动态刷新的列表(RecyclerView)、滑动刷新(SwipeRefreshLayout)以及各种复杂...
【标题】"Android新浪微博"揭示了本项目的核心——在Android平台上构建一个运行于虚拟机之上的新浪微博客户端。这个客户端能够使用户在移动设备上浏览、发布、互动与新浪微博相关的各种内容,实现社交网络的便捷使用...
《PullToZoomListViewEx在实现新浪微博第三方登录界面中的应用》 在移动开发中,为了提供更丰富的用户体验,许多开发者会采用特殊交互效果来提升应用的吸引力。新浪微博的第三方登录界面就是一个很好的例子,其中...
《Android应用源码解析——基于四次元新浪微博客户端》 在Android开发领域,学习和研究开源项目是提升技能的重要途径。本次我们关注的是"四次元新浪微博客户端项目",这是一份基于Android平台的微博客户端源码,它...
这个应用是针对中国流行的社交媒体平台——新浪微博(Sina Weibo)开发的。"www.weibo4android" 可能是该应用的在线主页或者开发者社区的链接,便于用户获取更多相关信息。 【描述】提到,这个应用是由一个大牛...
1. **用户界面(UI)设计**:Android Studio中的布局管理器如LinearLayout、RelativeLayout、ConstraintLayout等将被用于创建各种屏幕元素,如输入框、按钮、列表视图等,以构建用户友好的界面。 2. **网络通信**:...
本篇文章将深入探讨一个特殊的项目——"Android仿新浪微博客户端",它旨在为开发者提供一个参考实现,帮助他们理解和构建类似社交媒体应用。本文将从源代码结构、主要功能模块、关键技术和设计模式等方面进行详细...
首先,从UI设计的角度看,AnjoyoSinaWeibo源码展示了如何使用Android的布局管理器(如LinearLayout、RelativeLayout、ConstraintLayout)以及自定义View来构建用户界面。此外,还会涉及到主题和样式设置,以提供一致...