【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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章