马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.布局文件的配置:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="60dip"
android:id="@android:id/tabs"
android:background="#ffccdd"
></TabWidget>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="60dp"
android:id="@android:id/tabcontent"
></FrameLayout>
</RelativeLayout>
</TabHost>
</LinearLayout>
2.选项的配置:item.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_opt_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tab1"
/>
<TextView
android:id="@+id/tv_opt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缓存清理"
android:textColor="#99000000"
android:textSize="12sp"
/>
</LinearLayout>
3.部分activity布局的配置:activity_m1.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缓存清理"
></TextView>
</LinearLayout>
4.部分activity布局的载入:M1.javapackage com.cb.tabhost;
import com.example.tabhost_test.R;
import android.app.Activity;
import android.os.Bundle;
public class M1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
}
}
5.主程序功能的实现: MainActivity.java
package com.example.tabhost_test;
import com.cb.tabhost.M1;
import com.cb.tabhost.M2;
import com.cb.tabhost.M3;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost myTabHost = getTabHost();
TabSpec tab1 = myTabHost.newTabSpec("缓存清理");
tab1.setIndicator(getIndicatorView("缓存清理", R.drawable.tab1));
tab1.setContent(new Intent(this, M1.class));
TabSpec tab2 = myTabHost.newTabSpec("sdcard清理");
tab2.setIndicator(getIndicatorView("sdcard清理", R.drawable.tab2));
tab2.setContent(new Intent(this, M2.class));
TabSpec tab3 = myTabHost.newTabSpec("开机加速");
tab3.setIndicator(getIndicatorView("开机加速", R.drawable.tab3));
tab3.setContent(new Intent(this, M3.class)); //这个是底下要显示的界面
//将各项加入到tabHost里面
myTabHost.addTab(tab1);
myTabHost.addTab(tab2);
myTabHost.addTab(tab3);
myTabHost.setCurrentTab(0); //设置当前默认的显示位置是第一位
}
//这个返回的是一个转化后的布局文件
public View getIndicatorView(String name, int id)
{
View view = View.inflate(this, R.layout.item, null);
TextView tv = (TextView) view.findViewById(R.id.tv_opt_name);
ImageView iv = (ImageView) view.findViewById(R.id.iv_opt_item);
tv.setText(name);
iv.setImageResource(id);
return view;
}
}
|