【android开发】自定义TabHost

很多情况下,我们需要用到标签,类似于微末下面的标签效果,直接使用系统默认的往往不能满足项目的需求,就需要我们自定义一个TabHost。下面我把在项目的实现的效果分享给大家,希望能帮助需要的朋友!先上图看看效果,我把实现TabHost重新写在一个工程了,不是很好,看看效果吧!


下面ba 代码贴出来,实现起来比较简单,就不详细说明了!

新建一个视图文件main.xml

---------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/title"
   android:gravity="center_vertical">
   <ImageView
       android:id="@+id/title_backBtn"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10dp"
       android:src="@drawable/refreshbtn" />
   <ImageView
       android:id="@+id/title_setting"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:layout_marginRight="10dp"
       android:src="@drawable/settingbutton" />
</RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
<FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />
        <RadioGroup
                android:id="@+id/main_tab_group"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@drawable/main_navigation_background"
                android:gravity="bottom"
                android:orientation="horizontal" >
                <RadioButton
                    android:id="@+id/new_task_activity"
                    style="@style/main_tab_bottom"
                    android:checked="true"
                    android:drawableTop="@drawable/new_task"
                    android:text="未处理任务" />
                <View
       android:layout_width="1dip"
       android:layout_height="match_parent"
       android:background="#ff00ff66" />
                <RadioButton
                    android:id="@+id/handling_task_activity"
                    style="@style/main_tab_bottom"
                    android:drawableTop="@drawable/handling_task"
                    android:text="处理中任务" />
                <View
       android:layout_width="1dip"
       android:layout_height="match_parent"
       android:background="#ff00ff66" />
                <RadioButton
                    android:id="@+id/return_task_activity"
                    style="@style/main_tab_bottom"
                    android:drawableTop="@drawable/return_task"
                    android:text="回退任务" />
                <View
       android:layout_width="1dip"
       android:layout_height="match_parent"
       android:background="#ff00ff66" />
                <RadioButton
                    android:id="@+id/photoupload_activity"
                    style="@style/main_tab_bottom"
                    android:drawableTop="@drawable/photo_upload"
                    android:text="图片管理" />
            </RadioGroup>
    </LinearLayout>
</TabHost>

------------------------------------------------------------------------------------------------

在MainActivity添加代码:

--------------------------------------------------------------------------------------------------

package com.xinhui.mytablehost;


import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;


@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

private RadioGroup radioGroup;
private TabHost mTabHost;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//隐去标题栏(应用程序的名字)  
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.main_tab_group);
mTabHost = getTabHost();
//Intent用于activity之间通信
intent = new Intent(this,ItemActivity.class);
//Tab页跳转切换操作,切换到不同页面
mTabHost.addTab(mTabHost.newTabSpec("未处理任务").setIndicator("")
.setContent(intent));
mTabHost.addTab(mTabHost.newTabSpec("处理中任务").setIndicator("")
.setContent(intent));
mTabHost.addTab(mTabHost.newTabSpec("退回任务").setIndicator("")
.setContent(intent));
mTabHost.addTab(mTabHost.newTabSpec("图片上传").setIndicator("")
.setContent(intent));
intent.putExtra("tabhost", 0);
mTabHost.setCurrentTab(0);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

switch (checkedId) {
case R.id.new_task_activity://未处理任务
intent.putExtra("tabhost", 0);
Log.e("info", "未处理任务"+0);
mTabHost.setCurrentTab(0);
break;
case R.id.handling_task_activity://处理中任务
intent.putExtra("tabhost", 1);
Log.e("info", "处理中任务"+1);
mTabHost.setCurrentTab(1);
break;
case R.id.return_task_activity://退回任务
intent.putExtra("tabhost", 2);
Log.e("info", "退回任务"+2);
mTabHost.setCurrentTab(2);
break;
case R.id.photoupload_activity://图片上传
intent.putExtra("tabhost", 3);
Log.e("info", "图片上传"+3);
mTabHost.setCurrentTab(3);
break;
default:
break;
}
}
        });
}
}

------------------------------------------------------------------------------------------------------------

为实现一点效果,新建一个activity,来展示一下效果ItemActivity.java:

-------------------------------------------------------------------------------------------

package com.xinhui.mytablehost;


import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.TextView;


public class ItemActivity extends Activity {

private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
textView = (TextView) findViewById(R.id.tv);
//接收MainActivity传来的信息
int getID = getIntent().getIntExtra("tabhost", 0);
if(getID == 0){
textView.setText("未处理任务");
}

}  
@Override      
protected void onResume() {          
super.onResume();          
int getID = getIntent().getIntExtra("tabhost", 0);
if(getID == 0){
textView.setText("未处理任务");
}else if(getID == 1){
textView.setText("处理中任务");
}else if(getID == 2){
textView.setText("回退任务");
}else if(getID == 3){
textView.setText("图片管理");
}
Log.e("info", "onResume~~~"+getID);
}  
}

-------------------------------------------------------------------------------

核心代码主要就是上面的几个文件了,当然了,只写上面的代码,你会发现程序会报很多错误的,因为在工程中还建立其他的文件,比如一些自定义点击事件等,大家可以把源码下载下来跑一下,取出相关的图片和文件即可!


      源码下载http://download.csdn.net/detail/lixinhuixin/6420671


发布了38 篇原创文章 · 获赞 7 · 访问量 22万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章