首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置)

<TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"></FrameLayout><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="60dp"android:layout_weight="0"android:background="@drawable/bottom_bar"></TabWidget></LinearLayout></TabHost>

其次,分别作四个菜单选项的selector文件。状态为选中和默认:

<selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:state_selected="true" android:drawable="@drawable/tab_weixin_pressed"></item><item android:drawable="@drawable/tab_weixin_normal"></item>
</selector>

注意:处于选中状态的菜单,底部还会有个发光的背景,也是将背景设置为selector,代码如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:state_selected="true" android:drawable="@drawable/tab_bg"></item>
</selector>

再次,在代码中得到tabhost对象,并设置对应的四个不同的tab页,代码如下:

public class MainActivity extends android.app.TabActivity
{//继承TabActivityString[] name = {"微信","通讯录","朋友们","设置"};//写好四个selector文件,内容为选中状态和默认状态时的图片int[] imaResid = {R.drawable.selector_tab1,R.drawable.selector_tab2,R.drawable.selector_tab3,R.drawable.selector_tab4};ArrayList<TabSpec> arrayList = new ArrayList<TabHost.TabSpec>();@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_tab);TabHost tabHost = getTabHost();for (int i = 0; i < 4; i++){//复用布局,创建四个View的对象,并且分别设置View对象里的控件值和图片View tabrl = getLayoutInflater().inflate(R.layout.tab_item, null);TextView tv_name = (TextView) tabrl.findViewById(R.id.tv_name);tv_name.setText(name[i]);ImageView iv_icon = (ImageView) tabrl.findViewById(R.id.iv_icon);iv_icon.setImageResource(imaResid[i]);TabSpec spec = tabHost.newTabSpec("tab" + i).setIndicator(tabrl);arrayList.add(spec);}tabHost.addTab(arrayList.get(0).setContent(new Intent(MainActivity.this,Tab1Activity.class)));tabHost.addTab(arrayList.get(1).setContent(new Intent(MainActivity.this,Tab2Activity.class)));tabHost.addTab(arrayList.get(2).setContent(new Intent(MainActivity.this,Tab3Activity.class)));tabHost.addTab(arrayList.get(3).setContent(new Intent(MainActivity.this,Tab4Activity.class)));}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.tab, menu);return true;}
}

转载于:https://blog.51cto.com/wangcuijing/1282140

android 中使用TabHost控件实现微信界面的底部菜单效果相关推荐

  1. Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar

    文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...

  2. Android中进度条控件使用

    android中进度条控件使用 ProgressBar pb = findViewById(R.id.pb);pb.setMax(100);pb.setProgress(33); 转载于:https: ...

  3. 从零开始学android:Android中的基本控件(上)

    从零开始学android:Android中的基本控件(上) 本章内容较多,下面只贴代码,大家只需要贴到自己eclipse里就知道作用^^! View组件简介 Android中的View组件包含了几乎所 ...

  4. Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar

    文章目录 1 CheckBox 1.1 CheckBox介绍 2 RadioButton 2.1 RadioButton介绍 3 ToggleButton 3.1 ToggleButton介绍 4 S ...

  5. android中互斥的控件,Android控件之Radiobutton与RadioGroup

    RadioButton 是一个单选控件,在一个RadioGroup中,各个RadioButton是互斥的 XML文件: xmlns:tools="http://schemas.android ...

  6. android 中的组合控件的设计

    在开发应用程序的时候,很多时候会使用到几个重复的控件,例如Android手机的设置界面里面的位置服务里面的每一栏都是组合控件,也就是说多个控件组成一个整体,如下图所示: 红色方框里面的是由两个Text ...

  7. Android中如何使控件保持固定宽高比

    目录 1.自定义view 2.adjustViewBounds 3.百分比布局 4.ConstraintLayout 我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但 ...

  8. android中的 listview控件,聊聊Android中的ListView控件

    软硬件环境 Macbook Pro MGX 72 Android Studio 1.3.2 坚果手机 前言 ListView是Android系统中使用非常广泛的一种控件,几乎所有的App都会用到它.它 ...

  9. Android中的常用控件之进度条(ProgressBar)

    ProgressBar的常用属性 style(进度条的样式,默认为圆形:用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...

最新文章

  1. 再见,Python!!
  2. Spring Cloud【Finchley】实战-04将订单微服务与商品微服务分别拆分为多模块
  3. [AGC014D] Black and White Tree(树形DP,博弈)
  4. rpa文件怎么提取内容_怎么编辑pdf文件内容?有什么软件可以编辑pdf文件吗?
  5. Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
  6. ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2
  7. UI自动化测试 appium简介
  8. 【英语学习】【Level 07】U02 Live Work L1 Welcome to my home
  9. 字段不显示 继承_Springboot Shiro页面按钮显示、路径越权访问题
  10. HDOJ-1002 A + B Problem II C语言
  11. canvas的beginPath和closePath分析总结,包括多段弧的情况
  12. 算法:回溯十七 Combination Sum III挑选数组中规定个数元素的和为指定数
  13. UNIX和类Unix操作系统
  14. 前端系列教程之JS(自认为有用代码)
  15. 部署kubernetes
  16. 【180629】VC++ QQ头像连连看游戏源码
  17. 有意思的DCDC工作原理
  18. 组合数学+概率,期望+生成函数一文全精通
  19. 菜鸟网管的入门之路-第一章、网络及硬件篇(1)
  20. 大白映射 宝马Esys编程设码映射、USB设备映射

热门文章

  1. ORM是进化还是倒退?
  2. MySQL主主(双主)数据同步
  3. JS获取客户端IP地址、MAC和主机名的7个方法汇总
  4. Android Training - Volley(Lesson 0 - 序言)
  5. 库克在白宫网络峰会中再次反对加密后门
  6. 快速生成apk 自动发布到网站 便于测试
  7. NET 应用架构指导 V2 学习笔记(二十) 业务组件设计指导
  8. 三层交换机实现不同vlan间的通信
  9. Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践
  10. linux shell 中的sleep命令