好记性不如铅笔头

android, 编程

Android应用开发笔记:tabhost的两种实现方式

android界面开发中有两种实现tabhost的方式:一种是使用android自带的FragmentTabHost,另外一种是使用actionbarsherlock来实现。下面分别备份下两种的实现方式。

CONTENTS

github:

https://github.com/cstriker1407/android/tree/master/HelloLibABS 】

备注:

代码比较乱,里面有很多功能。

Android自带的FragmentTabhost:

activity_second.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp" />
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

SecondActivity.java文件:

public class SecondActivity extends SlidingFragmentActivity
{
	private FragmentTabHost tabHost;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setSlidingActionBarEnabled(false);
		
		setContentView(R.layout.activity_second);
		setBehindContentView(R.layout.activity_leftmenu);

		SlidingMenu sm = getSlidingMenu();
		sm.setBehindWidth(300);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		//从这里开始为tabhost的使用。上面为slidmenu的demo
		tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
		tabHost.setup(this, getSupportFragmentManager(), R.id.base);
		tabHost.addTab(tabHost.newTabSpec("left").setIndicator("dd"),LeftFrag.class, null);
		tabHost.addTab(tabHost.newTabSpec("right").setIndicator("xx"),RightFrag.class, null);
	}
}

 备注:

1)FragmentTabHost和FrameLayout的id必须设置为如上id,不能修改。

2)tabhost作者没有实际使用过,只是备份下最简单的实现方式。

actionbarsherlock:

activity_third.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

ThirdActivity.java:

public class ThirdActivity extends SherlockFragmentActivity implements TabListener
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_third);
		
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		ActionBar.Tab newTab = getSupportActionBar().newTab();
		newTab.setText("left");
	    newTab.setTabListener(this);
		getSupportActionBar().addTab(newTab);
		
		ActionBar.Tab newTab2 = getSupportActionBar().newTab();
		newTab2.setText("right");
	    newTab2.setTabListener(this);
		getSupportActionBar().addTab(newTab2);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft)
	{
		if (tab.getText().equals("left"))
		{
			ft.replace(R.id.frame, new LeftFrag());
		}
		if (tab.getText().equals("right"))
		{
			ft.replace(R.id.frame, new RightFrag());
		}
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft)
	{
		
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft)
	{
		
	}
}

备注:

1)作者当前并没有实际使用tabhost,这里只是备份下最简单的使用方式。

附上demo的几张截图:

更新:

如果actionbarsherlock支持tabhost,那么android自带的actionbar应该也可以支持tabhost,作者没有做过实现,不过猜想API应该和sherlock差不多。

发表评论

8 + 20 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据