- 浏览: 980002 次
- 性别:
- 来自: 深圳
-
博客专栏
-
-
飞雪的Android学习总...
浏览量:146231
文章分类
最新评论
-
lovebingheji:
感谢,看完了
Spring方法注入 -
ruijin5566:
package concurrent;
import ja ...
淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
helonghui:
Nginx在高并发的时候,内存开销比Apache更加有优势!
使用Nginx搭建PHP服务器 -
xjgpeople:
不错,写的非常不错
基于Android的浮动组件,可以用于应用中的新功能展示等等。 -
Bj_junxia:
不允许加入了,呜呜呜。。。。
Android系列教程之五:Activity的生命周期
前言
为了更好的开发Android应用程序,除了熟练掌握基本的UI组件和API外,还需要掌握一些技巧,而这些技巧可以通过阅读一些代码来提高,本系列将与大家分享一些新浪微博布局方面的收获,欢迎交流!
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://www.cnblogs.com/over140
版本
新浪微博 weibo_10235010.apk
正文
一、效果图
红色部分是本文要实现的目标。
二、实现maintabs.xml
<?xml version="1.0" encoding="UTF-8"?> <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" /> <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" /> <RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content"> <RadioButton android:text="@string/main_home" android:checked="true" android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" /> <RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" /> <RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" /> <RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" /> <RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" /> </RadioGroup> </LinearLayout> </TabHost>
styles.xml
<style name="main_tab_bottom"> <item name="android:textSize">@dimen/bottom_tab_font_size</item> <item name="android:textColor">#ffffffff</item> <item name="android:ellipsize">marquee</item> <item name="android:gravity">center_horizontal</item> <item name="android:background">@drawable/home_btn_bg</item> <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:button">@null</item> <item name="android:singleLine">true</item> <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item> <item name="android:layout_weight">1.0</item> </style>
home_btn_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" /> <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" /> <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" /> <item android:drawable="@drawable/transparent" /> </selector>
代码说明:
1. 需要注意的是他这里把TabWidget的Visibility设置成了gone!也就是默认难看的风格不见了:
,取而代之的是5个带风格的单选按钮.
2. 注意为单选按钮设置的style,其中最重要的是为其background设置了home_btn_bg.xml,也就是自定义了选中效果。
Java文件
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener { private TabHost mHost; private Intent mMBlogIntent; private Intent mMoreIntent; private Intent mInfoIntent; private Intent mSearchIntent; private Intent mUserInfoIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.maintabs); // ~~~~~~~~~~~~ 初始化 this.mMBlogIntent = new Intent(this, HomeListActivity.class); this.mSearchIntent = new Intent(this, SearchSquareActivity.class); this.mInfoIntent = new Intent(this, MessageGroup.class); this.mUserInfoIntent = new Intent(this, MyInfoActivity.class); this.mMoreIntent = new Intent(this, MoreItemsActivity.class); initRadios(); setupIntent(); } /** * 初始化底部按钮 */ private void initRadios() { ((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(this); ((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(this); ((RadioButton) findViewById(R.id.radio_button2)).setOnCheckedChangeListener(this); ((RadioButton) findViewById(R.id.radio_button3)).setOnCheckedChangeListener(this); ((RadioButton) findViewById(R.id.radio_button4)).setOnCheckedChangeListener(this); } /** * 切换模块 */ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { switch (buttonView.getId()) { case R.id.radio_button0: this.mHost.setCurrentTabByTag("mblog_tab"); break; case R.id.radio_button1: this.mHost.setCurrentTabByTag("message_tab"); break; case R.id.radio_button2: this.mHost.setCurrentTabByTag("userinfo_tab"); break; case R.id.radio_button3: this.mHost.setCurrentTabByTag("search_tab"); break; case R.id.radio_button4: this.mHost.setCurrentTabByTag("more_tab"); break; } } } private void setupIntent() { this.mHost = getTabHost(); TabHost localTabHost = this.mHost; localTabHost.addTab(buildTabSpec("mblog_tab", R.string.main_home, R.drawable.icon_1_n, this.mMBlogIntent)); localTabHost.addTab(buildTabSpec("message_tab", R.string.main_news, R.drawable.icon_2_n, this.mInfoIntent)); localTabHost.addTab(buildTabSpec("userinfo_tab", R.string.main_my_info, R.drawable.icon_3_n, this.mUserInfoIntent)); localTabHost.addTab(buildTabSpec("search_tab", R.string.menu_search, R.drawable.icon_4_n, this.mSearchIntent)); localTabHost.addTab(buildTabSpec("more_tab", R.string.more, R.drawable.icon_5_n, this.mMoreIntent)); } private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) { return this.mHost .newTabSpec(tag) .setIndicator(getString(resLabel), getResources().getDrawable(resIcon)) .setContent(content); }
代码说明
1. 由于TabWidget被隐藏,所以相关的事件也会无效,这里取巧用RadioGroup与RadioButton的特性来处理切换,然后监听事件调用setCurrentTabByTag来切换Activity。
2. 注意即使TabWidget被隐藏,也要为其设置indicator,否则会保持。
三、总结
在这之前如果要做这种效果我恐怕第一时间就会想到用ActivityGroup来做,主要是因为TabHost的TabWidget非常难看,用起 来也不方便。其实从源码可以看出,TabActivity也是继承自ActivityGroup,这里结合了单选按钮和TabHost,各取其长,有时间 可以专门写一个这样的自定义控件:)
四、相关文章
[Android]使用ActivityGroup来切换Activity和Layout
结束
本文中使用的资源均反编译自apk文件,这里主要是讲思路,欢迎大家交流。
评论

发表评论
-
android新建大分辨率模拟器不能启动的问题
2015-03-08 17:36 0新建的Android模拟器的分辨率超过一定大小的时候,就无法启 ... -
android覆盖式引导
2015-03-08 17:35 0我们在开发产品的时 ... -
Android产品开发中常用的一些开源项目
2015-03-08 17:27 2255你在工作中可能会遇到同时给你说不要重复发明轮子,其实这个说的 ... -
Android HttpClient Session保持
2015-03-08 17:20 14576现在单机版本的App已经 ... -
java.lang.UnsupportedOperationException android.view.GLES20Canvas.clipPath
2015-03-08 17:18 2134今天自定义控件使用Canvas绘图的时候遇到的这个错误,看错 ... -
解决Android SDK Manager不能更新的问题
2015-03-08 17:16 3253今天打算更新到Android4.3,看看里面的新的API,D ... -
PenddingIntent.getActivity
2013-03-11 16:40 0PenddingIntent.getActivity -
关于Android的Holo主题
2013-02-22 23:47 8166Android曾经为了优化用户体验,把原生的 ... -
Android Layout布局文件里的android:layout_height等属性为什么会不起作用?
2013-01-29 00:19 20607有的时候,我们配置好的布局文件,在加载完成添加到我们 ... -
震动反馈
2013-01-10 20:58 0震动反馈 -
Android ViewGroup.setDescendantFocusability函数
2013-01-05 12:15 22490这个函数是在ViewGroup里定义的,主要用于控制child ... -
Android设计应用图标不用愁---Asset Studio Integration来帮你
2011-11-12 00:18 8229Android Asset StudioWeb版是 ... -
最新最全的Android4.0 API源代码下载和完整Android4.0源代码下载教程
2011-11-15 09:41 2337这时刚刚整理好的最新的,包含所有的API的源代码,第一次上传的 ... -
基于Android的浮动组件,可以用于应用中的新功能展示等等。
2011-12-10 17:12 4744前言 在开发Android应用时,加新功能是必不可少 ... -
提取出的最新Android4.0 API 源代码
2011-11-15 14:06 4072提取出的Android4.0 API 的源代码,也就是andr ... -
Android中我为什么发不了邮件--Android邮件发送详解
2011-07-19 22:50 15710版权所有@飞雪无情,转载请著名出处:http:/ ... -
Android Developer和Google Group可以正常访问了
2011-06-21 08:56 2393Android Developer和Google Group可 ... -
Android中关于线程使用的几点注意事项
2011-05-21 22:43 8739版权所有:飞雪无情 ... -
Android系列教程之十二:Intents and Intent Filters(三)
2011-03-29 10:02 13029接上节继续。。版权所有:飞雪无情,转载请注明出处: ... -
android.resource://这个Uri你知道吗
2011-03-28 11:22 10297转自:http://www.android123.com.cn ...
相关推荐
T型三电平+SVPWM的下垂控制与双闭环中点电位平衡控制.pdf
STM32真实企业级项目:锅炉控制器源码、原理图与PCB图.pdf
STM32F103 Modbus主站源码:正常使役,支持多从机功能码通信及从机寄存器写入.pdf
Simulink永磁同步直驱风机PMSG一次调频离散模型:含虚拟惯性与下垂控制,可扩展至光伏储能研究.pdf
VSG仿真、并网与离网运行仿真、预同期并网控制及虚拟同步机逆变器仿真.pdf
VIC水文模型全程视频教学指导.pdf
vrep_coppeliasim+matlab机器人轨迹控制仿真:利用matlab读取轨迹并控制机械臂在墙上绘图的详细学习示例.pdf
2000-2022年上市公司行业异质性数据(技术密集型、劳动密集型、资本密集型)(含原始数据和处理代码) 1、时间:2000-2022年 2、指标:股票代码、年份、股票简称、统计日期、行业名称、行业代码、成立日期、上市日期、所在省份、所在城市、上市状态、保留两位行业代码、保留一位行业代码、高科技为1,非高科技为0、重污染为1,非重污染为0、制造业为1,非制造业为0、劳动密集型为1,资本密集型为2,技术密集型为3 3、来源:csmar 4、根据2012年中国证监会行业划分是否高科技、是否重污染、是否制造业、是否劳动密集型、资本密集型、技术密集型。 5、内容:包括原始数据、处理代码和计算结果
TMS320F28335电机控制程序:BLDC、PMSM无感有感及异步VF程序源代码与开发资料大全.pdf
tc275、s12x、s32k144基于CANoe的UDS诊断数据库CDD文件及CAPL Boot上位机、下位机程序移植说明文档.pdf
STM32系列通信透传技术:以太网、串口、CAN透传及OBD协议解析.pdf
STM32开发:IIR带阻滤波器设计与实现.pdf
UG后处理:CNC西门子828D后处理与西门子后处理工厂实战自用.pdf
MYSQL深入学习总结.pdf
Stewart六自由度平台反解算法 C#.pdf
1、文件说明: Centos8操作系统vim-ale-3.3.0-1.el8.rpm以及相关依赖,全打包为一个tar.gz压缩包 2、安装指令: #Step1、解压 tar -zxvf vim-ale-3.3.0-1.el8.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm
tc275、s12x和s32k144的Boot程序及UDS故障诊断与Bootloader移植的Python自制上位机源码.pdf
SSA-CNN-LSTM时间序列预测(Matlab)_ 麻雀算法优化卷积长短期记忆网络.pdf
UI篇:C#工控上位机Chart控件实现与展示.pdf
SRM12-8开关磁阻电机,功率2200w,额定转速3450rpm.pdf