使用ActivityGroup来实现tabhost效果

来源:互联网 发布:淘宝店铺的层级 编辑:程序博客网 时间:2024/05/16 02:30

MainActivity

package com.opencourse;import android.app.ActivityGroup;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TabHost;public class MainActivity extends ActivityGroup{private RadioGroup group;private TabHost tabHost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);group = (RadioGroup)findViewById(R.id.main_radio);tabHost=(TabHost) findViewById(R.id.tabhost);tabHost.setup(getLocalActivityManager());Intent allCourse=new Intent(this,CourseAllActivity.class);tabHost.addTab(tabHost.newTabSpec("全部").setIndicator("全部").setContent(allCourse));Intent pushCourse=new Intent(this,PushCourseActivity.class);tabHost.addTab(tabHost.newTabSpec("推荐").setIndicator("推荐").setContent(pushCourse));Intent category=new Intent(this,CategoryActivity.class);tabHost.addTab(tabHost.newTabSpec("分类").setIndicator("分类").setContent(category));Intent story=new Intent(this,StoryActivity.class);tabHost.addTab(tabHost.newTabSpec("收藏").setIndicator("收藏").setContent(story));Intent settingReference=new Intent(this,SettingReferenceActivity.class);tabHost.addTab(tabHost.newTabSpec("设置").setIndicator("设置").setContent(settingReference));group.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.radio_button0:tabHost.setCurrentTab(0);break;case R.id.radio_button1:tabHost.setCurrentTab(1);break;case R.id.radio_button2:tabHost.setCurrentTab(2);break;case R.id.radio_button3:tabHost.setCurrentTab(3);break;case R.id.radio_button4:tabHost.setCurrentTab(4);break;default:break;}}});tabHost.setCurrentTab(0);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, 1, Menu.NONE, "退出").setIcon(getResources().getDrawable(android.R.drawable.ic_lock_power_off));menu.add(Menu.NONE, 2, Menu.NONE, "关于").setIcon(getResources().getDrawable(android.R.drawable.ic_menu_help));menu.add(Menu.NONE, 3, Menu.NONE, "设置").setIcon(getResources().getDrawable(R.drawable.setting));return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case 1:alert();break;case 2:Intent intent=new Intent(MainActivity.this,AboutActivity.class);startActivity(intent);break;case 3:Intent setintent=new Intent(MainActivity.this,SettingReferenceActivity.class);startActivity(setintent);break;default:break;}return super.onOptionsItemSelected(item);}/** * 在ActivityGroup中捕获keyBack */@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){alert();return true;}return super.dispatchKeyEvent(event);}private void alert() {Builder builder=new AlertDialog.Builder(MainActivity.this);builder.setIcon(getResources().getDrawable(android.R.drawable.ic_dialog_info));builder.setTitle("你确定要退出吗?");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {finish();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});builder.create().show();}@Overrideprotected void onPause() {super.onPause();}}


main.xml

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bk" >    <!-- 内容垂直布局 -->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        >        <!-- 显示具体内容 -->        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="0.0dip"            android:layout_weight="1.0" >        </FrameLayout>        <!-- 选项卡 -->        <TabWidget            android:id="@android:id/tabs"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="0.0"            android:visibility="gone" >        </TabWidget>        <!-- 在低端显示 水平方向显示 -->                <RadioGroup            android:id="@+id/main_radio"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:background="@drawable/maintab_toolbar_bg"            android:gravity="center_vertical"            android:orientation="horizontal" >            <RadioButton                android:id="@+id/radio_button0"                style="@style/main_tab_bottom"                android:layout_marginTop="2.0dip"                android:drawableTop="@drawable/but1"                android:tag="radio_button0"                android:text="全部"/>            <RadioButton                android:id="@+id/radio_button1"                style="@style/main_tab_bottom"                android:layout_marginTop="2.0dip"                android:drawableTop="@drawable/but2"                android:tag="radio_button1"                android:text="推荐"/>            <RadioButton                android:id="@+id/radio_button2"                style="@style/main_tab_bottom"                android:layout_marginTop="2.0dip"                android:drawableTop="@drawable/but3"                android:tag="radio_button2"                android:text="分类"/>            <RadioButton                android:id="@+id/radio_button3"                style="@style/main_tab_bottom"                android:layout_marginTop="2.0dip"                android:drawableTop="@drawable/but4"                android:tag="radio_button3"                android:text="收藏"/>            <RadioButton                android:id="@+id/radio_button4"                style="@style/main_tab_bottom"                android:layout_marginTop="2.0dip"                android:drawableTop="@drawable/but5"                android:tag="radio_button4"                android:text="设置"/>        </RadioGroup>            </LinearLayout></TabHost>


该布局文件参照了sina微博的布局文件

需要注意的是在ActivityGroup或TabActivity中捕获keyBack需要覆写dispatchKeyEvent方法

/** * 在ActivityGroup中捕获keyBack */@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){alert();return true;}return super.dispatchKeyEvent(event);}


 

private void alert() {Builder builder=new AlertDialog.Builder(MainActivity.this);builder.setIcon(getResources().getDrawable(android.R.drawable.ic_dialog_info));builder.setTitle("你确定要退出吗?");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {finish();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});builder.create().show();}

同样为了使软键盘和底部的tab选项卡完美工作需要在AndroidManifest.xml文件中为TabActivity或ActivityGroup做如下设置


 

<activity android:name=".MainTabActivity"             android:configChanges="orientation|keyboardHidden"            android:windowSoftInputMode="adjustPan"            />


 

原创粉丝点击