Tab虽然不再是Android推荐,但其实用性还是得到开发者的喜爱。
本例程实现了底部tab,随选择切换底色,图标和文字颜色
实现Tab需要一个规则,具体如下:
activity_main.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/bg_edittext_normal">
</TabWidget>
</RelativeLayout>
</TabHost>
tab.xml
<?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" >
<ImageView android:id="@+id/tab_iv_icon"
android:layout_width="match_parent"
android:layout_height="30dp"
android:scaleType="fitCenter"
android:layout_marginTop="6dp" />
<TextView android:id="@+id/tab_tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:ellipsize="marquee"
android:gravity="center"
android:singleLine="true"
android:marqueeRepeatLimit="1"/>
</LinearLayout>
代码:
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
private TabHost tabHost;
private static final String HOME = "tab1";
private static final String REFER = "tab2";
private static final String ABOUT = "tab3";
private Intent tab1Intent;
private Intent tab2Intent;
private Intent tab3Intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost=this.getTabHost();
tabHost.setFocusable(true);
prepareIntent();
setupIntent();
tabHost.setOnTabChangedListener(new OnTabChangedListener());
tabHost.setCurrentTab(0);
updateTab(tabHost);
}
class OnTabChangedListener implements TabHost.OnTabChangeListener {
@Override
public void onTabChanged(String tabId) {
tabHost.setCurrentTabByTag(tabId);
updateTab(tabHost);
}
}
private void setupIntent(){
tabHost.addTab(buildTabSpec(HOME,R.drawable.img1,tab1Intent));
tabHost.addTab(buildTabSpec(REFER,R.drawable.img2, tab2Intent));
tabHost.addTab(buildTabSpec(ABOUT,R.drawable.img3, tab3Intent));
}
private TabHost.TabSpec buildTabSpec(String tag, int icon, Intent intent) {
View view = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView)view.findViewById(R.id.tab_iv_icon)).setImageResource(icon);
((TextView)view.findViewById(R.id.tab_tv_text)).setText(tag);
return tabHost.newTabSpec(tag)
.setIndicator(view)
.setContent(intent);
}
private void prepareIntent() {
tab1Intent=new Intent(this, tab1Activity.class);
tab2Intent=new Intent(this, tab2Activity.class);
tab3Intent=new Intent(this, tab3Activity.class);
}
private void updateTab(final TabHost tabHost) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
View view = tabHost.getTabWidget().getChildAt(i);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tab_tv_text);
ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tab_iv_icon);
//tv.setTextSize(16);
//tv.setTypeface(Typeface.SERIF, 2); // 设置字体和风格
if (tabHost.getCurrentTab() == i) {//选中
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector));//选中后的背景
switch (i)
{
case 0:
iv.setImageDrawable(getResources().getDrawable(R.drawable.img1));
break;
case 1:
iv.setImageDrawable(getResources().getDrawable(R.drawable.img2));
break;
case 2:
iv.setImageDrawable(getResources().getDrawable(R.drawable.img3));
break;
}
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.holo_green_dark));
} else {//不选中
view.setBackgroundDrawable(getResources().getDrawable(android.R.color.white));//非选择的背景
switch (i)
{
case 0:
iv.setImageDrawable(getResources().getDrawable(R.drawable.img12));
break;
case 1:
iv.setImageDrawable(getResources().getDrawable(R.drawable.img21));
break;
case 2:
iv.setImageDrawable(getResources().getDrawable(R.drawable.img31));
break;
}
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.black));
}
}
}
}