首先是布局:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/titlebar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="5dp"
android:background="@color/orange"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/linear_two_subjects"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/tv_two_subjects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textColor="#000000"
android:textSize="18sp"
android:gravity="center"
android:text="科目二 "/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear_three_subjects"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/tv_three_subjects"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="科目三"
android:textSize="18sp"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_divider" />
</LinearLayout>
</TabHost>
效果是这样的:
然后是Activity里的代码 ,不过已经通知废弃TabActivity这个类了。 现在用FragmentActivity
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import com.haha.tcpjspx.R;
import com.wanzheng.driver.yuyuexueche.orderTab_Activity.JiShiJiChengOrderListActivity;
/**
* 计时计程订单页面
*/
public class JiShiJiChengActivity extends TabActivity implements View.OnClickListener {
private TabHost mTabHost;
private LinearLayout linearThree;
private LinearLayout linearTwo;
private TextView tvTwo;
private TextView tvThree;
private int position=0;
public static final String TAB_THREE = "THREE_SUBJECTS";
public static final String TAB_TWO = "TWO_SUBJECTS";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ji_shi_ji_cheng);
initView();
}
private void initView() {
RelativeLayout titlebar = (RelativeLayout) findViewById(R.id.titlebar);
TextView txt = (TextView) titlebar.findViewById(R.id.titlebar_tv);
txt.setText("我的订单");
ImageView titleR = (ImageView) titlebar.findViewById(R.id.titlebar_back);
titleR.setVisibility(View.VISIBLE);
titleR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
// 科目三
linearThree = (LinearLayout) findViewById(R.id.linear_three_subjects);
// 科目二
linearTwo = (LinearLayout) findViewById(R.id.linear_two_subjects);
tvTwo = (TextView) findViewById(R.id.tv_two_subjects);
tvThree = (TextView) findViewById(R.id.tv_three_subjects);
mTabHost = getTabHost();
Intent i_two = new Intent(this, JiShiJiChengOrderListActivity.class);
Intent i_three = new Intent(this, JiShiJiChengOrderListActivity.class);
i_two.putExtra("type",2);
i_two.putExtra("position",0);
i_three.putExtra("type",3);
i_three.putExtra("position",0);
mTabHost.addTab(mTabHost.newTabSpec(TAB_TWO).setIndicator(TAB_TWO)
.setContent(i_two));
mTabHost.addTab(mTabHost.newTabSpec(TAB_THREE).setIndicator(TAB_THREE)
.setContent(i_three));
linearTwo.setOnClickListener(this);
linearThree.setOnClickListener(this);
if (position==1){
mTabHost.setCurrentTabByTag(TAB_TWO);
tvThree.setTextColor(getResources().getColor(R.color.white));
tvTwo.setTextColor(getResources().getColor(R.color.black));
}else if (position==2){
mTabHost.setCurrentTabByTag(TAB_THREE);
tvTwo.setTextColor(getResources().getColor(R.color.white));
tvThree.setTextColor(getResources().getColor(R.color.black));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.linear_two_subjects:
mTabHost.setCurrentTabByTag(TAB_TWO);
tvTwo.setTextColor(getResources().getColor(R.color.black));
tvThree.setTextColor(getResources().getColor(R.color.white));
break;
case R.id.linear_three_subjects:
mTabHost.setCurrentTabByTag(TAB_THREE);
tvTwo.setTextColor(getResources().getColor(R.color.white));
tvThree.setTextColor(getResources().getColor(R.color.black));
break;
default:
break;
}
}
}
写的不怎么好, 只是简单的使用 , 如果写的哪里有错的话请大神帮忙指出! 多谢了!