Android学习笔记_41_TabHost自定义标签和TraceView性能测试

1、页面布局:

  |        |        |        |       |--->这个部分叫TabWigdet   用来放标签的,每一个格格
  |----------------------------------|    放一个标签           
  |                                  |
  |                                  |-->这个部分叫FrameLayout,是一个
  |                                  |   用来显示每个标签页的内容的窗口
  |                                  |   
  |-----------------------------------
  以上整个的合起来叫做:TabHost

  使用TabHost 标签,不同的tab页可以看成是叠加在一起的界面,因此使用帧布局。在这些tab也的外面需要使用TabWidget包裹。

xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        
        

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            
            <LinearLayout
                android:id="@+id/page1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="这是第一个标签页" />
            LinearLayout>
            
            
            <LinearLayout
                android:id="@+id/page2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="这是第二个标签页" />
            LinearLayout>
            
            
            <LinearLayout
                android:id="@+id/page3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="这是第三个标签页" />
            LinearLayout>
        FrameLayout>
    LinearLayout>
TabHost>

2、自定义标签页和其动画背景:

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:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="1dp"
        android:background="@drawable/tab_bg"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="18sp" />
    

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

    <item android:drawable="@drawable/bg_selected" android:state_pressed="true"/>
    
    
    
    <item android:drawable="@drawable/bg_selected" android:state_selected="true"/>
    
    <item android:drawable="@drawable/bg_normal"/>
 

selector>

3、后台代码:

package com.example.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.os.Debug;
import android.view.Gravity;
import android.view.Menu;
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
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //traceview性能测试
        Debug.startMethodTracing("traceview");
        // 找到TabHost的标签集合
        tabHost = (TabHost) this.findViewById(R.id.tabhost);
        tabHost.setup();// 这一句在源代码中,会根据findviewbyId()找到
        // 对应的TabWidget,还需要根据findViewById()找到这个TabWidget下面对应的标签页的
        // 内容.也就是FrameLayout这个显示控件.
        /*
         * 这里的第一个findviewbyId(),这个Id是系统指定的固定的id,可以查看api文档得到
         * 进入文档G:\android\android-sdk-windows\docs\reference\packages.html点击
         * Reference-->然后在左侧找到android-->点击然后选择:R.id-->找到tabs点击--> public static
         * final int tabs Since: API Level 1 Constant Value: 16908307
         * (0x01020013) 这里可以看到tabs代表的值,但是为了可读性好,还是给在布局文件main.xml中给
         * TagWidget控件,用这种方式定义id: android:id="@android:id/tabs"这里@代表
         * 访问R文件,这里代表访问的是android包中的R文件中的id 这个类中的tabs
         * android是个包,R文件的类就在这个包中定义的,id是R类中的一个内部类, FrameLayout控件的id代表的是:
         * android:id="@android:id/tabs"
         */
        // TabSpec这个是标签页对象.
        TabSpec tabSpec = tabHost.newTabSpec("page1");// 新建一个标签页对象.
        // tabSpec.setIndicator("首页",getResources().getDrawable(R.drawable.i1));
        // 第一个参数指定标签名字,第二个,指定图片资源,汉子显示在图片的下面.
        tabSpec.setIndicator(createTabView("首页"));// 设置这个标签页的标题
        // createTabView("首页")这里这个时候就可以替换成自己的API,也就那个切换标签页的显示内容页对应的view对象
        tabSpec.setContent(R.id.page1);// 指定标签页的内容页.
        tabHost.addTab(tabSpec);// 把这个标签页,添加到标签对象tabHost中.

        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();
    }

    // 这里可以做一个自定义标签页,返回一个view,tabSpec.setIndicator(createTabView("第二页"))
    // 因为这里可以传一个view进去.
    private View createTabView(String name) {
        // 通过下面的这句可以得到自定义的标签页的view对象.
        // View tabView = getLayoutInflater().inflate(R.layout.tab, null);
        // TextView textView =tabView.findViewById(R.id.name);//找到textview控件
        // textView.setText(name);显示这个名称.
        // return tabView

        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;
    }

    @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;
    }

}

4、进行TraceView性能测试,加入写入SDK权限。

你可能感兴趣的:(Android,基础)