码迷,mamicode.com
首页 > 移动开发 > 详细

Android TabHost底部显示

时间:2014-12-09 17:34:16      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   os   使用   

刚开始从网上找了一些代码,使用的是TabActivity,但运行的时候总是异常退出,后来看帮助说4.0版TabActivity已过时,要用Fragment替代。。。。然后修来改去。。。。。。一直不能正常运行。。。。。。

后来下定决心还是用TabActivity去试试,一个个错误去查找,终于好了。。。。。。。。

布局:main_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
 <!-- ID要用android系统的ID -->
   <TabHost android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabhost" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="3dp"> <!-- 内容:ID要用android系统的ID --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/main_radio" android:layout_weight="1" android:background="#FFF" > </FrameLayout> <!-- 标签:ID要用android系统的ID --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <!-- 按钮 --> <RadioGroup android:id="@+id/main_radio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ccc" android:orientation="horizontal" android:layout_alignParentBottom="true"> <RadioButton android:id="@+id/tab_btn_quality" style="@style/main_tab_bottom" android:checked="false" android:text="@string/menu_quality" /> <RadioButton android:id="@+id/tab_btn_sop" style="@style/main_tab_bottom" android:checked="true" android:text="@string/menu_sop" /> <RadioButton android:id="@+id/tab_btn_video" style="@style/main_tab_bottom" android:checked="false" android:text="@string/menu_video" /> <RadioButton android:id="@+id/tab_btn_logout" style="@style/main_tab_bottom" android:checked="false" android:text="@string/menu_logout" /> </RadioGroup> </RelativeLayout> </TabHost> </RelativeLayout>

 

样式:style.xml

<style name="main_tab_bottom">
        <item name="android:textSize">13sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:gravity">center_horizontal</item>
        
        <item name="android:paddingTop">5dp</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1.0</item>
        
        <item name="android:button">@null</item>
        
        <item name="android:layout_marginTop">1.0dip</item>
        <item name="android:paddingBottom">5.0dip</item>
        
        <item name="android:background">@drawable/selector_tab_btn</item>
</style>

 

按钮背景:selector_tab_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <!-- 选择时背景 -->   
    <item android:drawable="@drawable/tab_bg_sel" android:state_checked="true" />
    
    <!-- 默认状态背景 -->
    <item android:drawable="@drawable/tab_bg"></item>

</selector>

 

代码:MainActivity.java

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.content.Intent;
import android.content.res.Resources;

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

	public static TabHost mTabHost;	
	public static TabHost getmTabHost()
	{
		return mTabHost;
	}
	
	private RadioGroup mMain_radiogroup;
	private RadioButton mTab_btn_quality,mTab_btn_sop,mTab_btn_video,mTab_btn_logout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab);
		
		Resources resources = getResources();
		mTabHost = getTabHost();
				
		//去掉圆角边线,好像没啥用,界面中看不出什么
		TabWidget tabWidget = mTabHost.getTabWidget();
		tabWidget.setStripEnabled(false);
		
		//添加标签页 
		mTabHost.addTab(mTabHost.newTabSpec(resources.getString(R.string.menu_tag_quality)).setIndicator(resources.getString(R.string.menu_tag_quality)).setContent(new Intent(this, Tab_Quality.class)));
		mTabHost.addTab(mTabHost.newTabSpec(resources.getString(R.string.menu_tag_sop)).setIndicator(resources.getString(R.string.menu_tag_sop)).setContent(new Intent(this, Tab_SOP.class)));
		mTabHost.addTab(mTabHost.newTabSpec(resources.getString(R.string.menu_tag_video)).setIndicator(resources.getString(R.string.menu_tag_video)).setContent(new Intent(this, Tab_Video.class)));
		//mTabHost.addTab(mTabHost.newTabSpec(resources.getString(R.string.menu_tag_logout)).setIndicator(resources.getString(R.string.menu_tag_logout)).setContent(new Intent(this, LoginActivity.class)));
		
		//关联控件与变量
		mMain_radiogroup = (RadioGroup)findViewById(R.id.main_radio);
		mTab_btn_quality = (RadioButton)findViewById(R.id.tab_btn_quality);
		mTab_btn_sop = (RadioButton)findViewById(R.id.tab_btn_sop);
		mTab_btn_video = (RadioButton)findViewById(R.id.tab_btn_video);
		mTab_btn_logout = (RadioButton)findViewById(R.id.tab_btn_logout);
		
		//单选钮选择事件
		mMain_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				
				Resources resources = getResources();
				if(checkedId == mTab_btn_quality.getId())
				{
					mTabHost.setCurrentTabByTag(resources.getString(R.string.menu_tag_quality));
				}
				else if(checkedId == mTab_btn_sop.getId())
				{
					mTabHost.setCurrentTabByTag(resources.getString(R.string.menu_tag_sop));
				}
				else if(checkedId == mTab_btn_video.getId())
				{
					mTabHost.setCurrentTabByTag(resources.getString(R.string.menu_tag_video));
				}
				else if(checkedId == mTab_btn_logout.getId())
				{
					//mTabHost.setCurrentTabByTag(resources.getString(R.string.menu_tag_logout));
					//退出到登录界面
					Intent intent = new Intent();
					intent.setClass(MainActivity.this, LoginActivity.class);
					startActivity(intent);
					MainActivity.this.finish();
				}
			}
		});
		
		//设置程序默认打开的标签
		mTabHost.setCurrentTabByTag(resources.getString(R.string.menu_tag_sop));		
	}

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

}

至于tab里面实际的内容,直接新建几个Activity就可以了。

然后运行就可以了。。。。

Android TabHost底部显示

标签:android   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/yuchongjike/p/4153490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!