首页 > 代码库 > 赵雅智_名片夹(6)_仿微信底导航栏

赵雅智_名片夹(6)_仿微信底导航栏

效果图如下

使用TabHost布局,并使用单选按钮组和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" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <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="0.0dip"
                android:layout_weight="1.0"
                android:background="@color/daohang_text" >
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.0"
                android:visibility="gone" >
            </TabWidget>

            <RadioGroup
                android:id="@+id/main_radiogroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@drawable/daohang_bg"
                android:gravity="center"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/RadioButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:background="@drawable/xml_tab_backgruod"
                    android:button="@null"
                    android:drawableLeft="@drawable/xml_tabselect_bg1"
                    android:gravity="center"
                    android:paddingLeft="10dp"
                    android:text="@string/daohang_cards"
                    android:textColor="@color/color_radiobutton" />

                <RadioButton
                    android:id="@+id/RadioButton0"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_weight="1.0"
                    android:background="@drawable/xml_tab_backgruod"
                    android:button="@null"
                    android:drawableLeft="@drawable/xml_tabselect_bg2"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="@string/daohang_person" />

                <RadioButton
                    android:id="@+id/RadioButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_weight="1.0"
                    android:background="@drawable/xml_tab_backgruod"
                    android:button="@null"
                    android:drawableLeft="@drawable/xml_tabselect_bg3"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="@string/daohang_change"
                    android:textColor="@color/color_radiobutton" />

                <RadioButton
                    android:id="@+id/RadioButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:background="@drawable/xml_tab_backgruod"
                    android:button="@null"
                    android:drawableLeft="@drawable/xml_tabselect_bg4"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="@string/daohang_setup"
                    android:textColor="@color/color_radiobutton" />
            </RadioGroup>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

Acyivity代码:

package com.cards.activity;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.cards.R;
import com.cards.commom.MyApp;

public class PremierAct extends TabActivity {
	public static Class mTabClassArray[] = { CardsListAct.class,
			MyCardAct.class, DiscoverAct.class, SetupAct.class };
	public static String title[] = { "名片夹", "个人", "交换", "设置" };
	private RadioGroup m_radioGroup;
	private TabHost m_tabHost;
	Intent intent;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_premier);
		// 用于安全退出
		MyApp.getInstance().addActivity(this);
		init();
		// 设置默认显示界面
		m_tabHost.setCurrentTabByTag(title[0]);
		intent = new Intent();
		startService(intent
				.setAction("com.cards.activity.service.HeartService"));
	}

	// 初始化TabHost
	private void init() {
		m_tabHost = getTabHost();
		int count = mTabClassArray.length;
		for (int i = 0; i < count; i++) {
			TabSpec tabSpec = m_tabHost.newTabSpec(title[i])
					.setIndicator(title[i]).setContent(getTabItemIntent(i));
			m_tabHost.addTab(tabSpec);
		}

		m_radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
		m_radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.RadioButton0:
					m_tabHost.setCurrentTabByTag(title[1]);
					break;
				case R.id.RadioButton1:
					m_tabHost.setCurrentTabByTag(title[0]);
					break;
				case R.id.RadioButton2:
					m_tabHost.setCurrentTabByTag(title[2]);

					break;
				case R.id.RadioButton3:
					m_tabHost.setCurrentTabByTag(title[3]);
					break;
				}
			}
		});
	}

	private Intent getTabItemIntent(int index) {
		Intent intent = new Intent(this, mTabClassArray[index]);
		return intent;
	}

	@Override
	protected void onStart() {
		// 用于安全退出
		MyApp.getInstance().addActivity(this);
		super.onStart();
	}

	/**
	 * 安全退出
	 */
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getRepeatCount() == 0) {
			// 具体的操作代码
			AlertDialog.Builder builder = new AlertDialog.Builder(
					PremierAct.this);
			builder.setMessage("确认退出吗?");
			builder.setTitle("温馨提示");
			builder.setPositiveButton("确认", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					ActivityManager activityMgr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
					activityMgr.restartPackage(getPackageName());
					stopService(intent
							.setAction("com.cards.activity.service.HeartService"));

					MyApp.getInstance().exit();
					System.exit(0);
					finish();
					// 结束后台服务、 end
				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			builder.create().show();
			return false;
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	protected void onDestroy() {
		// 退出时销毁当前activity时再销毁一次
		ActivityManager activityMgr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		activityMgr.restartPackage(getPackageName());
		MyApp.getInstance().exit();
		System.exit(0);
		stopService(intent.setAction("com.cards.activity.service.HeartService"));
		finish();
		super.onDestroy();
	}
}

按钮按下效果:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/daohang_cards" android:state_checked="false"/>
    <item android:drawable="@drawable/daohang_cards_put" android:state_checked="true"/>

</selector>

背景按下效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/daohang_bg_put" android:state_checked="true"/>

</selector>

 


赵雅智_名片夹(6)_仿微信底导航栏