概述
1、mainactivity 的layout:
<?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="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
</LinearLayout>
</LinearLayout>
</TabHost>
2、selector.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<View
android:id="@+id/tabIndicator"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#a70000" />
<View
android:id="@+id/tabDivider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#2b2e39" />
<LinearLayout
android:id="@+id/tabView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="7dp" >
<ImageView
android:id="@+id/tab_iv_icon"
android:layout_width="30px"
android:layout_height="30px"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tab_tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15.0sp" />
</LinearLayout>
</LinearLayout>
3、MainActivity
package com.example.tabhostdemo;
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.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity{
private TabHost tabHost;
private TabWidget tabWidget;
private int preTabId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = this.getTabHost();
tabWidget = this.getTabWidget();
tabHost.setFocusable(true);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int tId = Integer.parseInt(tabId);
if(tId != preTabId)
{
setTabBg(tId, true);
setTabBg(preTabId, false);
preTabId = tId;
}
}
});
Intent intent = new Intent(this, Activity1.class);
tabHost.addTab(buildTabSpec("0", "标签1", intent));
intent = new Intent(this, Activity2.class);
tabHost.addTab(buildTabSpec("1", "标签2", intent));
intent = new Intent(this, Activity3.class);
tabHost.addTab(buildTabSpec("2", "标签3", intent));
intent = new Intent(this, Activity4.class);
tabHost.addTab(buildTabSpec("3", "标签4", intent));
}
private TabSpec buildTabSpec(String tabId, String tag1, Intent intent) {
try {
View view = View.inflate(this, R.layout.selector, null);
((TextView) view.findViewById(R.id.tab_tv_text)).setText(tag1);
if(tabId.equals("0")){
view.findViewById(R.id.tabIndicator).setVisibility(View.VISIBLE);
view.findViewById(R.id.tabDivider).setVisibility(View.INVISIBLE);
} else{
view.findViewById(R.id.tabIndicator).setVisibility(View.INVISIBLE);
view.findViewById(R.id.tabDivider).setVisibility(View.VISIBLE);
}
return tabHost.newTabSpec(tabId).setIndicator(view).setContent(intent);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void setTabBg(int tabId, boolean isSelected){
System.out.println("tabId:" + tabId);
View myTabView = tabWidget.getChildTabViewAt(tabId);
View tabIndicator = myTabView.findViewById(R.id.tabIndicator);
View tabDivider = myTabView.findViewById(R.id.tabDivider);
View tabView = myTabView.findViewById(R.id.tabView);
if(isSelected){
tabIndicator.setVisibility(View.VISIBLE);
tabDivider.setVisibility(View.INVISIBLE);
tabView.setBackgroundColor(0x14161d);
} else{
tabIndicator.setVisibility(View.INVISIBLE);
tabDivider.setVisibility(View.VISIBLE);
tabView.setBackgroundColor(0x272c39);
}
}
}
最后
以上就是微笑唇彩为你收集整理的TabHost与Activity的全部内容,希望文章能够帮你解决TabHost与Activity所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复