33-Android标签页

/tabhost/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabhost"
    >
	<LinearLayout
	android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
	>
		<TabWidget
		android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:id="@android:id/tabs"  //由系统决定 查询api
		/>
		<FrameLayout
		android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:id="@android:id/tabcontent"  //由系统决定 查询api
		>
			<LinearLayout
			android:layout_width="fill_parent"
    		android:layout_height="fill_parent"
    		android:id="@+id/page1"
			>
			   <TextView
			   android:layout_width="fill_parent"
    			android:layout_height="wrap_content"
    			android:text="这是第一个标签页"
			   />
			</LinearLayout>
		
		   <LinearLayout
			android:layout_width="fill_parent"
    		android:layout_height="fill_parent"
    		android:id="@+id/page2"
			>
			   <TextView
			   android:layout_width="fill_parent"
    			android:layout_height="wrap_content"
    			android:text="这是第二个标签页"
			   />
			</LinearLayout>
			
		    <LinearLayout
			android:layout_width="fill_parent"
    		android:layout_height="fill_parent"
    		android:id="@+id/page3"
			>
			   <TextView
			   android:layout_width="fill_parent"
    			android:layout_height="wrap_content"
    			android:text="这是第三个标签页"
			   />
			</LinearLayout>
		</FrameLayout>
	</LinearLayout>
</TabHost>

自定义标签页/tabhost/res/layout/tab.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"
  android:background="#FFFFFF"
  >
  <TextView
  android:background="@drawable/tab_bg"  //配置状态显示
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:textSize="18sp"
  android:textColor="#FFFFFF"
  android:layout_marginRight="1dp"
  android:id="@+id/name"
  />
</LinearLayout>

/tabhost/res/drawable/tab_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg_selected" /> <!-- pressed -->
    <item android:state_selected="true" android:drawable="@drawable/bg_selected" /> 
    <item android:drawable="@drawable/bg_normal" /> <!-- default -->
</selector>

/tabhost/src/cn/itcast/tabhost/MainActivity.java

package cn.itcast.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.os.Debug;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends Activity {
	TabHost tabHost;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Debug.startMethodTracing("itcast");//开始数据采集

        tabHost = (TabHost) this.findViewById(R.id.tabhost);
        tabHost.setup();//找到widget和framelayout,xml中查询api设置id否则找不到
        
        TabSpec tabSpec = tabHost.newTabSpec("page1");
        //设置抬头 名称和图片 默认标签页
        //tabSpec.setIndicator("首页", getResources().getDrawable(R.drawable.i1));
        
        //自定义标签页
        tabSpec.setIndicator(createTabView("首页"));
        tabSpec.setContent(R.id.page1); //第一页内容
        tabHost.addTab(tabSpec);
        
        tabSpec = tabHost.newTabSpec("page2");
       // tabSpec.setIndicator("第二页", getResources().getDrawable(R.drawable.i2));
        tabSpec.setIndicator(createTabView("第二页"));
        tabSpec.setContent(R.id.page2);
        tabHost.addTab(tabSpec);
        
        tabSpec = tabHost.newTabSpec("page3");
        //tabSpec.setIndicator("第三页", getResources().getDrawable(R.drawable.i7));
        tabSpec.setIndicator(createTabView("第三页"));
        tabSpec.setContent(R.id.page3);
        tabHost.addTab(tabSpec);
        
        tabHost.setCurrentTab(0);//停留在第一页
    }
    
	@Override
	protected void onDestroy() {
		Debug.stopMethodTracing();//停止数据采集
		super.onDestroy();
	}

	private View createTabView(String name) {
		//View tabView = getLayoutInflater().inflate(R.layout.tab, null);
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		linearLayout.setBackgroundColor(0xFFFFFF);
		
		TextView textView = new TextView(this);
		textView.setText(name);
		textView.setBackgroundResource(R.drawable.tab_bg);
		textView.setTextColor(0xFFFFFF);
		textView.setTextSize(18.0f);
		textView.setGravity(Gravity.CENTER);
		ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		linearLayout.addView(textView, params);
		
		return linearLayout;
	}
}

数据采集以测试性能,一定要sdk权限,在启动和结束activity的时候进行采集sdk目录下traceview工具




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值