前言

  为了更好的开发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

 <selectorxmlns: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 implementsOnCheckedChangeListener {private TabHost mHost;private Intent mMBlogIntent;private Intent mMoreIntent;private Intent mInfoIntent;private Intent mSearchIntent;private Intent mUserInfoIntent;@Overrideprotected 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);}/*** 切换模块*/@Overridepublic 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文件,这里主要是讲思路,欢迎大家交流。

新浪微博布局学习——妙用TabHost相关推荐

  1. 新浪微博布局学习--新浪微博Android版底部工具栏的Demo

    Android开发技术交流群86686524    120059404 前几天看到一篇很好的文章<新浪微博布局学习--妙用TabHost>,已经转载到我的博客里新浪微博布局学习--妙用Ta ...

  2. Android学习笔记:TabHost 和 FragmentTabHost

    2019独角兽企业重金招聘Python工程师标准>>> Android学习笔记:TabHost 和 FragmentTabHostTabHost命名空间:android.widget ...

  3. Android学习笔记:TabHost 和 FragmentTabHost(转)

    Android学习笔记:TabHost 和 FragmentTabHost(转) 转自:http://www.cnblogs.com/asion/p/3339313.html 作者:Asion Tan ...

  4. CSS3与页面布局学习总结(八)——浏览器兼容与前端性能优化

    一.浏览器兼容 1.1.概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题.不同浏览器其内核亦不尽相同,相同内核的版本不同,相同版本的内核浏览器品牌不一样,各种运 ...

  5. CSS3与页面布局学习笔记(四)——页面布局大全(负边距、双飞翼、多栏、弹性、流式、瀑布流、响应式布局)

    一.负边距与浮动布局 1.1.负边距 所谓的负边距就是margin取负值的情况,如margin:-100px,margin:-100%.当一个元素与另一个元素margin取负值时将拉近距离.常见的功能 ...

  6. Android商城排版,android排版布局学习

    Android开发学习之路--UI之基本布局 android使用shape stroke描边只保留底部 RadioGroup的使用 一.项目粮仓 粮仓学习: liangcang: 启动页面:act_s ...

  7. 上海计算机一级ps教程,2017年计算机一级考试PS学习妙招

    经过一段时间的观察与实践,我觉得没有美工基础的人也能学好用好.因为普高的学生并不像单招的学生在高中时期是以专业为主要的课程,他们的美工基础自然是没有单招的全面.普高学生大多没有很好美工基础,但我相信, ...

  8. Flex布局 学习(二)

    上一篇<Flex布局 学习(一)>主要学了flex的基本用法及作用,现在来把flex具体运用到实例中,看看flex的弹性布局效果. 一.垂直居中布局 不知道有多少小伙伴在前端笔试面试时被问 ...

  9. CSS3与页面布局学习笔记(八)——浏览器兼容性问题与前端性能优化方案

    一.浏览器兼容 1.1.概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题.不同浏览器其内核亦不尽相同,相同内核的版本不同,相同版本的内核浏览器品牌不一样,各种运 ...

最新文章

  1. java读取ACCESS数据库的简单示例
  2. sparksql删除MySQL数据_Databricks 第6篇:Spark SQL 维护数据库和表
  3. LinkedList 模拟栈和队列
  4. 2019.5.8_此书真乃宝书也_从定位参数到仅限关键字参数
  5. mysql 怎么实现随机查询并分页,不重复查询
  6. html之meta标签
  7. Nutch1.2二次开发详细攻略(一)【图文】------Windows平台下Cygwin环境的搭建
  8. CodeProject是个好地方
  9. matlab 命令打开ANSYS,matlab如何调用ansys
  10. YUV格式的图片查看工具YUView 2.13
  11. cad统计面积长度插件vlx_cad计算总长度插件
  12. 4K高清屏幕保护工具Aquarium 4K for Mac
  13. 红米k30 android版本,红米K30系列机型众多,傻傻分不清楚?看完这篇你就懂了
  14. KioptrixVM3-writeup
  15. Mac OS和Office版本问题
  16. 【译文】工作六年后,我对软件开发的认知转变
  17. SSD固态硬盘的安装优化点点收集
  18. 国际期货投机有什么风险?目前正大怎么样?
  19. 计算机一级wps练习题,计算机一级WPS练习题
  20. HTML会跳动的爱心特效

热门文章

  1. vue中的key的作用?
  2. unity物体自身轴旋转_unity3D实现物体任意角度自旋转
  3. 十大3D立体游戏强烈推荐
  4. 适合前端学习的设计模式有哪些?
  5. python抢券代码_用Python写一个京东抢券脚本
  6. Nginx 教程-动静分离
  7. 火山PC抓取快递物流查询接口教程第三课
  8. Ubuntu20.04 安装HPC_SDK加速库
  9. cad管道阀门符号dwg_cad管道阀门符号怎么画(CAD怎么画系统图,特殊符号怎么搞,像管道系统图,...)...
  10. #include memory.h如何使用