本文主要介绍Tabhost加载4个Activity及自定义的底部导航,效果如图所示:
下面开始介绍主要的实现流程:
1.利用最普通的Tabhost完成Activity的加载
2.在Tabhost布局中将TabWidget的visibility设为gone,就是不让该组件显示,但还要用到他的属性
3.在代码中实现底部导航栏
具体实现代码:
xml布局文件activity_main如下
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/linearLayout" 在代码中会实例化该LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff"
android:orientation="vertical">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" 注意这里为1,下面底部导航的weight为11,这样可以保证导航在底部
android:layout_gravity="fill">
</FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" 这里属性设为gone, 替代他的布局在代码中编写
android:layout_alignParentBottom="true">
</TabWidget>
</LinearLayout>
</TabHost>
MainActivity中的代码:
package com.example.test_tabhost;
import java.util.ArrayList;
import java.util.List;
import android.R.color;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends TabActivity {
private TabHost tabHost;
private LinearLayout ll;
private List default_imagelist;
private List list1= new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.linearLayout);
tabHost = this.getTabHost(); //得到TabHost实例
//在TabWidget中加入四个可以切换的按钮
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator(null,null) //设置tab的标题
.setContent(new Intent(this,Activity1.class)));//设置tag的内容
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator(null,null)
.setContent(new Intent(this, Activity2.class)));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator(null,null)
.setContent(new Intent(this, Activity3.class)));
tabHost.addTab(tabHost.newTabSpec("tab4")
.setIndicator(null,null)
.setContent(new Intent(this, Activity4.class)));
SetIndexButton();//自定义底部导航的实现及调用tabweiget切换Activity的方法
// tabHost.setCurrentTabByTag("tab1"); //默认选中左边第一个
tabHost.setCurrentTab(0);//默认选中左边第一个
}
public void SetIndexButton(){
LinearLayout bottom = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lp.weight=11;//weight为11保证该布局在底部
bottom.setLayoutParams(lp);
bottom.setPadding(8, 8, 8, 8);
bottom.setBackgroundColor(0xff24589d);
bottom.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(bottom);
getImageArray();//将按钮图片加入到数组中
LinearLayout.LayoutParams iv_lps = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
iv_lps.weight=1;
LinearLayout.LayoutParams tv_lps = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
tv_lps.weight=(float) 1.7;
for(int i = 0;i<4;i++){//底部导航四个按钮的实现
LinearLayout item = new LinearLayout(this);//每个按钮有一个图片和一个文字组成
item.setLayoutParams(iv_lps);
item.setOrientation(LinearLayout.VERTICAL);
item.setGravity(Gravity.CENTER);
item.setTag(i);
bottom.addView(item);
ImageView iv = new ImageView (this);
iv.setLayoutParams(iv_lps);
iv.setImageResource((Integer) default_imagelist.get(i));
item.addView(iv);
TextView tv = new TextView(this);
tv.setLayoutParams(tv_lps);
tv.setText(""+(i+1));
tv.setGravity(Gravity.CENTER);
item.addView(tv);
if(i==0){//设置默认选中的按钮背景颜色
item.setBackgroundColor(Color.YELLOW);
tv.setTextColor(Color.RED);
list1.add(item);//将被选中的线性布局加入一个数组中,以便下次点击别的按钮时将他恢复到未点击状态
}else{
tv.setTextColor(Color.GRAY);
}
item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tabHost.setCurrentTab((Integer) v.getTag());//切换不同的Activity
setBackground(v);//设置导航按钮的点击效果
}
});
}
}
public void getImageArray(){//将你的图片放在这里
default_imagelist = new ArrayList();
default_imagelist.add(R.drawable.m_address);
default_imagelist.add(R.drawable.m_assess);
default_imagelist.add(R.drawable.m_cart);
default_imagelist.add(R.drawable.m_income);
}
public void setBackground(View v){
callback();//将上次被点击的线性布局恢复到未被选中的状态,并清空数组
LinearLayout ll1= (LinearLayout) v;
ll1.setBackgroundColor(Color.YELLOW);
ImageView iv= (ImageView) ll1.getChildAt(0);
TextView tv = (TextView) ll1.getChildAt(1);
tv.setTextColor(Color.RED);
list1.add(ll1);//将已被选中的线性布局加入到数组中以便以后恢复
}
public void callback(){
if(list1.size()!=0){
LinearLayout ll1 =(LinearLayout) list1.get(0);
ImageView iv= (ImageView) ll1.getChildAt(0);
TextView tv = (TextView) ll1.getChildAt(1);
ll1.setBackgroundColor(0xff24589d);
tv.setTextColor(Color.GRAY);
list1.clear();
}
}
}
四个Activity的实现都相同,改一下名字就ok(记得注册该Activity):
public class Activity1 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
TextView tv = new TextView (this);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setText("Activity1");//text值也改一下,区分不同Activity
setContentView(tv);
}
}
其实是个很简单很实用的demo,以后开发新项目有Tabhost的话就可以直接套用。
由于本人深恶痛绝下载源码还要财富的现象,现把源码放在这里http://yunpan.cn/QIWVFMFc4Fani和大家分享