新浪微博布局学习——活用RelativeLayout

简介:

正文

  一、效果图

    格子布局效果:

 

 (图一)

 居中正在加载的效果:

 

 (图二)

二、实现代码

    2.1  实现 图一 效果代码
 

             < RelativeLayout  android:id ="@id/rlDigest"
                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  实现 图二 效果代码

     < RelativeLayout  android:id ="@+id/rlpb"
        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_centerInParent   居中布局
android:layout_centerVertical    水平居中布局
android:layout_centerHorizontal  垂直居中布局

 

    android:layout_alignParentTop    居于容器内顶部
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一致



本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/582146,如需转载请自行联系原作者

相关文章
|
2月前
|
弹性计算 人工智能 Java
2026年10月最新!阿里云10款热门服务器配置排行榜:含价格、带宽、适用场景全解析
2026年10月阿里云服务器最新排行榜,涵盖轻量应用、ECS及GPU机型,按预算分三梯队(百元入门至万元AI算力),详解10款高性价比配置,含价格、适用场景与避坑指南,助你精准选型。
772 0
|
4月前
|
人工智能 Rust 开发工具
当 AI Agent 不再是 Bot,而是你的"同事"——我花两天拆解了 Block 开源的 Buzz
Buzz是Block开源的Rust协作平台,基于Nostr协议,首创“Agent即成员”范式:人类与AI Agent共用统一签名事件流,各自持独立密钥对,授权可验证、审计可追溯。架构极简——一张事件表承载聊天、代码、CI等全部操作,Git存储采用对象存储+CAS指针,支持高并发Agent协同。
527 1
1688“找工厂”运营提升指南:三大核心事项与落地执行清单
系统化运营是1688“找工厂”业务增长的关键。希望本清单能帮助商友明确方向,逐步推进店铺优化与升级。如有具体操作问题,欢迎在评论区交流探讨!
|
9月前
|
存储 运维 搜索推荐
2026年企业网站建设全流程实施指南
本文结合数字化转型背景,从核心原则、工具选型、全流程实施三方面,给出企业建站完整方案,助力品牌传播与业务转化。
799 0
2026年企业网站建设全流程实施指南
|
缓存 自然语言处理 数据可视化
阿里云百炼产品月刊【2025年7月】
阿里云百炼平台7月推出多项更新与活动,。通义大模型家族迎来五连发升级,推出Qwen-Plus-2025-07-28快照模型,支持1M上下文长度,新增Qwen3-Coder-Plus,具备强大Coding Agent能力。同时,通义万相2.2系列全面升级,文生图、文生视频能力大幅提升。多项模型限时优惠,Qwen3-Coder-Plus最高享5折,Qwen-Plus、Qwen-Turbo价格下调50%,Qwen-MT-Plus降价80%。应用模块新增UI设计器,支持可视化构建Web App。另有多种活动上线,包括Agent创客实训、Qwen3-Coder挑战赛及全栈焕新课程。
2155 2
|
10月前
|
人工智能 运维 数据可视化
万小智到底有多好用?阿里云建站全攻略:场景、优势、价格、FAQ 一文搞定
阿里云万小智AI建站是零代码自助建站平台,输入需求即可AI生成网站初稿,支持可视化编辑与一键发布。覆盖企业官网、营销落地页、轻量电商、个人作品集四大场景,集成AI设计、配图、客服、内容创作能力,提供全托管服务,免运维、高安全、快访问。
936 5
|
11月前
|
人工智能 JavaScript 前端开发
抛弃 Zapier?当 AI 接管业务流程,为什么 n8n 成了企业的“新基建”?
n8n为何成为企业级AI自动化首选:以工作流为载体,兼具代码灵活性与数据私密性,支持复杂逻辑编排,助力AI安全融入核心业务。
876 2
|
12月前
|
运维 监控 Linux
Linux 学习资源精选:从入门到运维的高效清单
本文针对Linux学习“资源繁杂、难筛选”的痛点,按入门、运维、进阶三阶段精选优质资源,拆解其核心价值与使用方法,结合实操建议与学习规划,帮助学习者分阶段精准提升,少走弯路,系统构建Linux知识体系。