前面简单介绍了选项卡,下面以及后面的几篇文章介绍下Android选项卡的几种简单实现方法。

http://blog.csdn.net/xia215266092/article/details/9613897

看到上面的最版本的QQ软件,整个软件的UI框架就是选项卡,一般想到的就是使用Android自带的TabActivity实现。

实现需要一个主界面,来存放选项卡,在布局中需要存放TabHost和TabWidget。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent" >
  6.  
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="vertical" >
  11.  
  12. <TabWidget
  13. android:id="@android:id/tabs"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:paddingLeft="1dip"
  17. android:paddingRight="1dip"
  18. android:paddingTop="4dip"
  19. android:visibility="gone" />
  20.  
  21. <FrameLayout
  22. android:id="@android:id/tabcontent"
  23. android:layout_width="fill_parent"
  24. android:layout_height="0dip"
  25. android:layout_weight="1" />
  26. <RadioGroup
  27. android:id="@+id/rg"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:background="#f00"
  31. android:orientation="horizontal" >
  32.  
  33. <RadioButton
  34. android:id="@+id/rb01"
  35. style="@style/menu_item"
  36. android:checked="true"
  37. android:drawableTop="@drawable/menu_home"
  38. android:text="主页" />
  39.  
  40. <RadioButton
  41. android:id="@+id/rb02"
  42. style="@style/menu_item"
  43. android:drawableTop="@drawable/menu_clear"
  44. android:text="清除" />
  45.  
  46. <RadioButton
  47. android:id="@+id/rb03"
  48. style="@style/menu_item"
  49. android:drawableTop="@drawable/menu_refresh"
  50. android:text="刷新" />
  51.  
  52. <RadioButton
  53. android:id="@+id/rb04"
  54. style="@style/menu_item"
  55. android:drawableTop="@drawable/menu_save"
  56. android:text="保存" />
  57.  
  58. <RadioButton
  59. android:id="@+id/rb05"
  60. style="@style/menu_item"
  61. android:drawableTop="@drawable/menu_more"
  62. android:text="更多" />
  63. </RadioGroup>
  64.  
  65. </LinearLayout>
  66.  
  67. </TabHost>

同时需要个Activity,这个Activity必须继承TabActivity,其实也不一定需要继承这个,可以继承ActivityGroup,自定义一个tabactivity,但是这样比较复杂,所以我们还是继承TabActivity吧。

  1. public class TabMainActivity extends TabActivity {
  2.  
  3. TabHost tab;
  4. Context context;
  5. RadioGroup rg;
  6.  
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.tab_main);
  10.  
  11. tab = getTabHost();
  12. context = this;
  13.  
  14. tab.addTab(tab.newTabSpec("A").setIndicator("A").setContent(new Intent(context, AActivity.class)));
  15. tab.addTab(tab.newTabSpec("B").setIndicator("B").setContent(new Intent(context, BActivity.class)));
  16. tab.addTab(tab.newTabSpec("C").setIndicator("C").setContent(new Intent(context, CActivity.class)));
  17. tab.addTab(tab.newTabSpec("D").setIndicator("D").setContent(new Intent(context, DActivity.class)));
  18. tab.addTab(tab.newTabSpec("E").setIndicator("E").setContent(new Intent(context, EActivity.class)));
  19.  
  20. rg = (RadioGroup) findViewById(R.id.rg);
  21. rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  22. public void onCheckedChanged(RadioGroup group, int checkedId) {
  23. int idx = -1;
  24. if (checkedId == R.id.rb01) {
  25. idx = 0;
  26. } else if (checkedId == R.id.rb02) {
  27. idx = 1;
  28. } else if (checkedId == R.id.rb03) {
  29. idx = 2;
  30. } else if (checkedId == R.id.rb04) {
  31. idx = 3;
  32. } else if (checkedId == R.id.rb05) {
  33. idx = 4;
  34. }
  35. switchActivity(idx);
  36. }
  37. });
  38. }
  39.  
  40. protected void switchActivity(int idx) {
  41. int n = tab.getCurrentTab();
  42.  
  43. if (idx < n) {
  44. tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
  45. } else if (idx > n) {
  46. tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
  47. }
  48. tab.setCurrentTab(idx);
  49. if (idx < n) {
  50. tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_in));
  51. } else if (idx > n) {
  52. tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
  53. }
  54.  
  55. RadioButton rb = (RadioButton) rg.getChildAt(idx);
  56. rb.setChecked(true);
  57. }
  58.  
  59. private Map<String, Object> data = new HashMap<String, Object>();
  60.  
  61. protected void put(String key, String value) {
  62. data.put(key, value);
  63. }
  64.  
  65. protected Object get(String key) {
  66. return data.get(key);
  67. }
  68. }

AActivity,BActivity,CActivity,DActivity,EActivity,都是点击选项卡,对应的Activity,这样UI框架,可以按照软件的功能来分模块,便于团队人员的合作和开发。

底部的菜单使用的RadioGroup,因在RadioGroup里面的有若干RadioButton,只有能有一个才能被选中,这样就很完美的实现的上面被选中的效果,Android有自定义Radiobutton选中的背景。

自定义一个Radiobutton

  1. <RadioButton
  2. android:id="@+id/rb01"
  3. style="@style/menu_item"
  4. android:checked="true"
  5. android:drawableTop="@drawable/menu_home"
  6. android:text="主页" />

然后在自定义一个style,让RadioButtong使用使用这个Style。

  1. <style name="menu_item">
  2. <item name="android:layout_width">fill_parent</item>
  3. <item name="android:layout_height">fill_parent</item>
  4. <item name="android:padding">10dp</item>
  5. <item name="android:button">@null</item>
  6. <item name="android:gravity">center_horizontal</item>
  7. <item name="android:background">@drawable/menu_item_bg</item>
  8. <item name="android:layout_weight">1</item>
  9. </style>

<item name="android:background">@drawable/menu_item_bg</item>

就是这个RadioButton的背景。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="false" android:state_checked="false" android:drawable="@drawable/menu_normal" />
  4. <item android:state_checked="true" android:drawable="@drawable/menu_press" />
  5. <item android:state_pressed="true" android:drawable="@drawable/menu_press" />
  6. </selector>

定义不同状态的显示的背景,当选中或者是被点击的时候,使用的背景menu_press,否则就是menu_normal。

好了,这篇先简单介绍在这里,后面还有后续。

浅谈Android选项卡(二)的更多相关文章

  1. 浅谈Android选项卡(四)

    前面几篇介绍的选项的用法,基本上使用TabActivity.ViewPager.已经基本上满足开发需求了.但是这里再介绍一种小技巧,在有的时候,感觉使用前面的ViewPager和Fragment时候, ...

  2. 浅谈Android选项卡(一)

    选项卡,这样UI设计在很多方面都存在,window,web,ios,Android. 选项卡的主要作用,不用多介绍,可以在有线的空间内,显示出更多内容,同时也是操作起来也很方便.

  3. 浅谈Android选项卡(三)

    上一节介绍了TabActivity的简单用法,但是现在的Api中已经不建议使用了,建议使用Fragment来替代以上的功能,下面介绍下使用Fragment和ViewPager的结合使用. http:/ ...

  4. [转]浅谈Android五大布局(二)——RelativeLayout和TableLayout

    在浅谈Android五大布局(一)中已经描述了LinearLayout(线性布局).FrameLayout(单帧布局)和AbsoulteLayout(绝对布局)三种布局结构,剩下的两种布局Relati ...

  5. 浅谈Android应用保护(一):Android应用逆向的基本方法

    对于未进行保护的Android应用,有很多方法和思路对其进行逆向分析和攻击.使用一些基本的方法,就可以打破对应用安全非常重要的机密性和完整性,实现获取其内部代码.数据,修改其代码逻辑和机制等操作.这篇 ...

  6. 安卓开发_浅谈Android动画(四)

    Property动画 概念:属性动画,即通过改变对象属性的动画. 特点:属性动画真正改变了一个UI控件,包括其事件触发焦点的位置 一.重要的动画类及属性值: 1.  ValueAnimator 基本属 ...

  7. 浅谈Android应用性能之内存

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/ jaunty [博主导读]在Android开发中,不免会遇到许多OOM现象,一方面可能是由于开 ...

  8. 浅谈Android五大布局

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLay ...

  9. 浅谈Kotlin(二):基本类型、基本语法、代码风格

    浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 通过上面的文章,在A ...

随机推荐

  1. 安装操作系统CentOS-7.x

    一.创建虚拟机 使用VMware Fusion创建虚拟机 二.系统安装 为了统一环境,保证实验的通用性,将网卡名称设置为eth*,不使用CentOS 7默认的网卡命名规则.所以需要在安装的时候,增加内 ...

  2. zabbix结合grafana

    一.下载grafana 下载地址: http://docs.grafana.org/installation/rpm/ https://s3-us-west-2.amazonaws.com/grafa ...

  3. GRUB使用说明

    从Red Hat Linux 7.2起,GRUB(GRand Unified Bootloader)取代LILO成为了默认的启动装载程序.相信LILO对于大家来说都是很熟悉的.这次Red Hat Li ...

  4. code1319 玩具装箱

    一个划分dp,不过由于划分个数任意,仅用一维数组就可以 设dp[i]表示前i个装箱(任意个箱子)的费用最小值 dp[i]=min(dp[u]+cost(u+1,i)) 但是n<=50000,n方 ...

  5. IntelliJ IDEA开发golang环境配置

    IntelliJ IDEA开发golang环境配置 首先把GO安装好...(自行安装,附上一篇我之前写的MAC安装GO) 安装IntelliJ IDEA,下载地址: https://www.jetbr ...

  6. myeclipse如何将项目打包成war包

    打包步骤如下: 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https://cnblogs.com/qzf/

  7. 研究wireshark遇到的问题

    说起来有一些惭愧,研究wireshark有一段时间了,但是对源代码的分析却至今没有什么进展... 最初想要研究wireshark是因为我的开题是基于wireshark来做的. 现在有很多抓包工具,wi ...

  8. SecondaryNameNode中的“Inconsistent checkpoint fields”错误原因

    该错误原因,可能是因为没有设置好SecondaryNameNode上core-site.xml文件中的"hadoop.tmp.dir". 2014-04-17 11:42:18,1 ...

  9. Android 文件存放路径【转】

    对于应用携带的静态数据,可以放置在应用的assets目录或者res,raw目录下.对于assets目录下的静态数据,存在当文件最大支持1MB的局限,读取方式如下: 1 InputStream is = ...

  10. 设计模式1---单例模式(Singleton pattern)

    单例模式Singleton 面试的时候,问到许多年轻的Android开发他所会的设计模式是什么,基本上都会提到单例模式,但是对 单例模式也是一知半解,在Android开发中我们经常会运用单例模式,所以 ...