安卓开发 底部tab的实现

主要代码

public class TabsFrame extends FragmentActivity {

private FragmentTabHost fragmentTabHost;
     private String texts[] = { "首页", "消息", "好友", "广场" };
     private int imageButton[] = { R.drawable.selector1,
             R.drawable.selector2,
             R.drawable.selector3,
             R.drawable.selector4};
     private Class fragmentArray[] = {Page1.class,
             Page2.class,
             Page3.class,
             Page4.class};

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabsframe);
    
            // 实例化tabhost
            fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            fragmentTabHost.setup(this, getSupportFragmentManager(),R.id.maincontent);
    
             for (int i = 0; i < texts.length; i++) {
                TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
                fragmentTabHost.addTab(spec, fragmentArray[i], null);
                 
                //设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常)
                fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bgselector);
              }
      }
    private View getView(int i) {
         //取得布局实例
         View view=View.inflate(TabsFrame.this, R.layout.tabcontent, null);
         
         //取得布局对象
         ImageView imageView=(ImageView) view.findViewById(R.id.image);
         TextView textView=(TextView) view.findViewById(R.id.text);
         
         //设置图标
         imageView.setImageResource(imageButton[i]);
         //设置标题
         textView.setText(texts[i]);
         return view;
    }
    public boolean onKeyDown(int KeyCode,KeyEvent Event){

if(KeyCode==KeyEvent.KEYCODE_BACK){
               Intent intent=new Intent(TabsFrame.this,MainActivity.class);
           startActivity(intent);
           finish();
            }
        return true;
    }
 
 }

Layout文件

  1. tabsframe

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >
     
          <!-- 存放主要页面内容 -->
     
          <FrameLayout
             android:id="@+id/maincontent"
             android:layout_width="fill_parent"
             android:layout_height="0dp"
             android:layout_weight="1" >
        </FrameLayout>
         
         <!-- 底层菜单 -->
     
         <android.support.v4.app.FragmentTabHost
             android:id="@android:id/tabhost"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:background="@drawable/tab_bg" >
     
             <FrameLayout
                android:id="@android:id/tabcontent"
                 android:layout_width="0dp"
                 android:layout_height="0dp"
                 android:layout_weight="0" >
             </FrameLayout>
        </android.support.v4.app.FragmentTabHost>
     </LinearLayout>

  2. tabcontent

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:orientation="vertical" >
 
      <ImageView
          android:id="@+id/image"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         />
     <TextView
         android:id="@+id/text"
         android:padding="2dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="@android:color/white"
         />
 </LinearLayout>

这样就可以实现底部菜单栏啦!

时间: 2024-07-30 13:48:55

安卓开发 底部tab的实现的相关文章

安卓开发复习笔记——TabHost组件(实现底部菜单导航)

安卓开发复习笔记——TabHost组件(实现底部菜单导航) 4 来源:cnblog    阅读:1048   时间:2014-09-16 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用windows操作系统的时候,经常见到如图所示的图形界面. TabHost选项卡,说到这个组件,不得不先说一件事情,翻翻谷歌提供给我们的API,我们可以发现这样的一段话: 它告诉我们,这个组件在安卓4.0之后已经被废弃了,建议我们新的程序应该使用Fragment组件

安卓开发复习笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航) 安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航) 关于Fragment类在之前的安卓开发复习笔记——Fragment+ViewPager组件(高仿微信界面)也介绍过,这里就不再重复阐述了. 国际惯例,先来张效果图: 下面

安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)

关于底部菜单是什么,我想没必要介绍了,在市场上的APP里太常见了,这里提供两种方式来实现. 记得之前写过几篇关于底部菜单实现的方法,有兴趣的朋友可以看看: 1.<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)> 2.<安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航)> 3.<安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)> 今天带来种相对更通俗易懂的写法,不再和过去一样去沿用TabHost了

安卓开发常见布局方式学习

在安卓开发中我们常用的布局方式有这么几种: 1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角) 线性布局分为水平线性和垂直线性二者的属性分别为: android:orientation= " horizontal " android:orientation= "vertical" . 2.RelativeLayout ( 相对布局 ) : (里面可以放多个控件,但是一行只能放一个控件) 附加几类

安卓开发中我们常用的布局方式

在安卓开发中我们常用的布局方式有这么几种: 1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角) 线性布局分为水平线性和垂直线性二者的属性分别为: android:orientation= " horizontal " android:orientation= "vertical" . 2.RelativeLayout ( 相对布局 ) : (里面可以放多个控件,但是一行只能放一个控件) 附加几类

首页-底部Tab导航(菜单栏)的实现:FragmentTabHost+ViewPager+Fragment

前言 Android开发中使用底部菜单栏的频次非常高,主要的实现手段有以下: - TabWidget - 隐藏TabWidget,使用RadioGroup和RadioButton - FragmentTabHost - 5.0以后的TabLayout - 最近推出的 Bottom navigation 今天带大家来探索下如何用Fragment+FragmentTabHost++ViewPager 实现底部菜单栏 目录 总体设计思路 Fragment:存放不同选项的页面内容 FragmentTab

安卓开发-设置RadioButton的点击效果

在安卓开发中用到底部菜单栏 需要用到RadioButton这个组件 实际应用的过程中,需要对按钮进行点击,为了让用户知道是否点击可这个按钮,可以设置点击后 ,该按钮的颜色或者背景发生变化. layout中这部分的代码为: 1 <RadioButton 2 android:id="@+id/radio_button0" 3 android:layout_height="fill_parent" 4 android:layout_marginTop="2

Android底部Tab页基于ViewPager的实现

在众多主流App中,包括QQ,微信等,为了和ios的UI保持统一,很多App使用的都是底部导航,当然在Android中也并不反对这种设计.这篇文章使用ViewPager实现这种效果.首先看实现效果图吧.效果图中包含了ViewPager的嵌套. 在讨论实现之前,我们先来回忆一下,之前我们是如何实现的. 之前的实现方法,我们无法是先进行布局,再编写逻辑.而布局,底部有一定高度的Tab,一般由4个或者5个,也有3个的,具体视应用而定,而Tab上方则是一个FrameLayout,用于作为fragment

Android自定义控件----RadioGroup实现APP首页底部Tab的切换

?[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4463931.html 联系方式:[email protected] [正文] 实现APP首页底部Tab的切换已经见过四五种方式了,先来看运行的效果图吧: 今天我们就用RadioGroup的方法来实现以下. [开发环境] 物理机版本:win 7旗舰版(64位) IDE版本:Android Stud