【最终效果】
【源代码】
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中有这一句跟没这一句,显示有差别,上图左边两个是对比。