【小项目】TabHost

【最终效果】

               

【源代码】

1.activity_main1.xml

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/view1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="view1...." />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/view2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="view2...." />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/view3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="view3...." />
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>

</TabHost>

2.MainActivty.java

package com.example.tabproject1;

import com.example.tabproject1.R.drawable;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class MainActivity extends Activity implements OnTabChangeListener {
	TabHost tabs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activty_main1);
		makeTabs();
	}

	public void makeTabs() {
		tabs = (TabHost) findViewById(android.R.id.tabhost);// 控件与资源id绑定(!R.id.tabhost报错)
		tabs.setup();
		TabHost.TabSpec spec1 = tabs.newTabSpec("tab1");// 初始显示
		
		spec1.setContent(R.id.view1);// 显示内容
		spec1.setIndicator("图书", getResources().getDrawable(R.drawable.hong));// tab显示的名称(!getResources().getDrawable(R.drawable.ic_launcher))
		tabs.addTab(spec1);
		
		TabHost.TabSpec spec2 = tabs.newTabSpec("tab2");
		spec2.setContent(R.id.view2);
		spec2.setIndicator("电影");
		tabs.addTab(spec2);
		
		tabs.addTab(tabs.newTabSpec("tab3")
		.setIndicator("音乐", getResources().getDrawable(R.drawable.ic_launcher))
		.setContent(R.id.view3));
		
		setTitle("wwy->tabhost");
		
        tabs.setBackgroundResource(R.drawable.ic_launcher);//设置背景
		tabs.setBackgroundColor(Color.argb(150, 22, 70, 150)); // 设置TabHost的背景颜色
		tabs.setCurrentTab(0);
		tabs.setOnTabChangedListener(this);
	}
	
	@Override
	public void onTabChanged(String tabId) {//设置动作
	if(tabId.equals("tab1")){
		Toast.makeText(this, "tab1", Toast.LENGTH_LONG).show();
	}else if(tabId.equals("tab2")){
		Toast.makeText(this, "tab2", Toast.LENGTH_LONG).show();
	}else{
		Toast.makeText(this, "tab3", Toast.LENGTH_LONG).show();
	}
	}

}

【知识点】

(1) android:id="@android:id/tabs"  引用时是:tabs = (TabHost) findViewById(android.R.id.tabhost);  

     // 控件与资源id绑定(!R.id.tabhost报错)

(2)TabHost.TabSpec spec1 = tabs.newTabSpec("tab1");这里的"tab1"并不能显示,设置点击事件时有用。

(3)spec1.setIndicator("图书", getResources().getDrawable(R.drawable.hong));

(4)TabHost的一些设置

        tabs.setBackgroundResource(R.drawable.ic_launcher);//设置背景
        tabs.setBackgroundColor(Color.argb(150, 22, 70, 150)); // 设置TabHost的背景颜色
        tabs.setCurrentTab(0);//默认显示第几个
        tabs.setOnTabChangedListener(this);//设置单击事件

    // 引用drawable资源的格式: getResources().getDrawable(R.drawable.ic_launcher)

  

【问题】

(1)虽然有这一句:

spec1.setIndicator("图书", getResources().getDrawable(R.drawable.hong));// tab显示的名称(!getResources().getDrawable(R.drawable.ic_launcher))

但是图片不显示,不知道怎么回事

(2)

AndroidManifest中有这一句跟没这一句,显示有差别,上图左边两个是对比。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值