Android UI, TabHost (过时)

1. Tabhost 也提供了一个 tabactivity 来方便开发, 所以有两种使用方式。

TabHostActivity.java

package com.example.uitest;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;

//方式一, 继承
//public class TabHostActivity extends TabActivity {
//方式二, 直接layout
public class TabHostActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
					
		//方式1 直接继承TabActivity
		showTabHost1();
		
		//方式2 直接layout
		setContentView(R.layout.layout_tabhost);
		showTabHost2();
	}

	//在布局文件中交换 TabWidget FrameLayout顺序,并设置weight 可以让tab到底下来
	private void showTabHost2() {
		TabHost tabHost2 = (TabHost) findViewById(R.id.tabhost);
		tabHost2.setup();
		
		//setContent时 如果传一个intent 试图去放入一个activity就报错   why?  
		TabSpec tabSpec1 = tabHost2.newTabSpec("tab1")
				.setIndicator("", getResources().getDrawable(R.drawable.tabhost_bg))
				.setContent(R.id.tab1);
		TabSpec tabSpec2 = tabHost2.newTabSpec("tab2")
				.setIndicator("", getResources().getDrawable(R.drawable.tabhost_bg))
				.setContent(R.id.tab2);
		tabHost2.addTab(tabSpec1);
		tabHost2.addTab(tabSpec2);
		
		
	}

	private void showTabHost1() {
//		TabHost tabHost1 = getTabHost();
//		TabWidget tabWidget = getTabWidget();		
//		//设置icon的时候就只能设置字串为"", 如果要显示文字和图标,可以做一个带文字的图标
//		//或者自顶一个View,   ImageView + TextView
//		TabSpec tabSpec1 = tabHost1.newTabSpec("tab1")
//				.setIndicator("", getResources().getDrawable(R.drawable.tabhost_bg))
//				.setContent(new Intent(this, ScrollActivity.class));
//		TabSpec tabSpec2 = tabHost1.newTabSpec("tab2")
//				.setIndicator("", getResources().getDrawable(android.R.drawable.ic_dialog_email))
//				.setContent(new Intent(this, MyActivity4.class));		
//		tabHost1.addTab(tabSpec1);
//		tabHost1.addTab(tabSpec2);
	}

}
layout_tabhost.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" >

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

        <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="match_parent"
                android:layout_weight="10.0" >

                <TextView
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="tab1" >
                </TextView>

                <TextView
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="tab2" >
                </TextView>
            </FrameLayout>
            
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.0" >
            </TabWidget>
        </LinearLayout>
    </TabHost>
    

</LinearLayout>
/res/drawable/tabhost_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
   <item android:state_selected="true" android:drawable="@drawable/button1"/>
   <item android:state_selected="false" android:drawable="@drawable/button2"/>
   
</selector>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值