Android标签菜单的实现

本文介绍如何在Android应用中实现标签类菜单,包括布局设计与Java代码实现,展示具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     不管是通信类的app还是新闻类的app,画面的底端都会出现一个标签类的导航框(如:QQ的信息、好友、动态的导航框),下面就对这种标签菜单的简单实现做一下介绍。

     实现这种标签有两种常用的方法。第一种是直接继承tabactivity类,不过这种方法在Android4.0后便被取代,而且实现起来较麻烦。所以这里就直接介绍第二种实现方法,通过tabhost组件来实现。

     首先是布局页面,在添加TabHost组件后需要在里面添加两个组件:TabWidget和FrameLayout,并且将TabWidget的id设为@android:id/tabs,将FrameLayout的id设为@android:id/tabcontent。TabWidget组件就是标签的列表组件,FrameLayout里面放置的是每个标签的具体内容。 

<RelativeLayout 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"
    tools:context=".ListActivity" >

    <TabHost 
        android:id="@+id/tabhost"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        >
        
        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="0dp"
            >
            
            <TabWidget
		        android:id="@android:id/tabs"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
    	    >
		</TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <LinearLayout 
                    android:id="@+id/tab1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <AnalogClock 
					android:layout_width="fill_parent"
					android:layout_height="wrap_content" 
                        />
                </LinearLayout>
                
                <LinearLayout 
                    android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    
                    <RatingBar 
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"/>
                    
               </LinearLayout>
                
              
              <LinearLayout 
                    android:id="@+id/tab3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
              
                      <EditText 
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="asdf"/>

                </LinearLayout>
			</FrameLayout>
            
    
        </LinearLayout>
    </TabHost>

   
    
</RelativeLayout>

 接下来是java代码

package com.sqm.myqq;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;

public class ListActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        
        TabHost tabHost = (TabHost)this.findViewById(R.id.tabhost);//获取TabHost组件
        tabHost.setup();//建立TabHost组件
        
        tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.tab1).setIndicator("First tab"));//向tabHost对象中添加TabSpec组件,tabSpec组件获取的方法为TabHost的newTabSpec()方法
        tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.tab2).setIndicator("Second tab"));
        tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.tab3).setIndicator("Third tab"));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list, menu);
        return true;
    }
    
}

 

    这样,界面中就会出现标签类的菜单组件了。

效果如下图:

 



 

 

 

 

 

 

 

Tag的使用 package com.yarin.android.qiehuan; import android.app.AlertDialog; import android.app.Dialog; import android.app.TabActivity; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class Activity01 extends TabActivity { //声明TabHost对象 TabHost mTabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost对象 mTabHost = getTabHost(); /* 为TabHost添加标签 */ //新建一个newTabSpec(newTabSpec) //设置其标签和图标(setIndicator) //设置内容(setContent) mTabHost.addTab(mTabHost.newTabSpec("test1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.textview1)); mTabHost.addTab(mTabHost.newTabSpec("test2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.textview2)); mTabHost.addTab(mTabHost.newTabSpec("test3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.textview3)); //设置TabHost的背景颜色 mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); //设置TabHost的背景图片资源 //mTabHost.setBackgroundResource(R.drawable.bg0); //设置当前显示哪一个标签 mTabHost.setCurrentTab(0); //标签切换事件处理,setOnTabChangedListener mTabHost.setOnTabChangedListener(new OnTabChangeListener() { // TODO Auto-generated method stub @Override public void onTabChanged(String tabId) { Dialog dialog = new AlertDialog.Builder(Activity01.this) .setTitle("善谢谢提醒") .setMessage("现在选中了:"+tabId+"标签") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }).create();//创建按钮 dialog.show(); } }); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值