UI___tab view 的实现
一.通过xml文件实现
1.创建一个tab iew,所需的xml文件
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:fadingEdge="none" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/LinearLayout01" android:drawingCacheQuality="high" android:layout_height="fill_parent" android:layout_width="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="false" android:layout_below="@android:id/tabs" android:layout_marginTop="-5dp"> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:tabStripEnabled="false" /> </RelativeLayout>
final TabHost tabs = (TabHost) this.findViewById(android.R.id.tabhost); final TabSpec tspec1 = tabs.newTabSpec("Journal [3]"); //设置tab的lable,跟icon tspec1.setIndicator("Journal [3]", getResources().getDrawable(R.drawable.navi_journal)); tspec1.setContent(new Intent(this, SubPageJournal.class)); tabs.addTab(tspec1); final TabSpec tspec2 = tabs.newTabSpec("Appointments"); tspec2.setIndicator("Appointments", getResources().getDrawable(R.drawable.navi_appointment)); tspec2.setContent(new Intent(this, SubPageAppointment.class)); tabs.addTab(tspec2); final TabSpec tspec3 = tabs.newTabSpec("My Lab"); tspec3.setIndicator("My Lab", getResources().getDrawable(R.drawable.navi_lab)); tspec3.setContent(new Intent(this, SubPageLab.class)); tabs.addTab(tspec3); final TabSpec tspec4 = tabs.newTabSpec("Medications"); tspec4.setIndicator("Medications", getResources().getDrawable(R.drawable.navi_medication)); tspec4.setContent(new Intent(this, SubPageMedications.class)); tabs.addTab(tspec4); //设置背景颜色 tabs.getTabWidget().getChildAt(0).setBackgroundColor(0xffDE2852); tabs.getTabWidget().getChildAt(1).setBackgroundColor(0xff007DC6); tabs.getTabWidget().getChildAt(2).setBackgroundColor(0xffFF9631); tabs.getTabWidget().getChildAt(3).setBackgroundColor(0xff94CB31); tabs.setBackgroundColor(0xffDE2852);<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF"> <RadioGroup android:id="@+id/RadioGroup01" android:background="#DE2852" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="5dp"> <RadioButton android:text="My Care" android:id="@+id/myCare" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ff000000" android:background="@drawable/buttonbackground" android:button="@drawable/no_image" android:layout_weight="1" android:gravity="center_horizontal" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"></RadioButton> <RadioButton android:text="Vital" android:id="@+id/vital" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ff000000" android:background="@drawable/buttonbackground" android:button="@drawable/no_image" android:layout_weight="1" android:gravity="center_horizontal" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"></RadioButton> <RadioButton android:text="Givers" android:id="@+id/givers" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ff000000" android:background="@drawable/buttonbackground" android:button="@drawable/no_image" android:layout_weight="1" android:gravity="center_horizontal" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"></RadioButton> <RadioButton android:text="History" android:id="@+id/history" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ff000000" android:background="@drawable/buttonbackground" android:button="@drawable/no_image" android:layout_weight="1" android:gravity="center_horizontal" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"></RadioButton> </RadioGroup> <ListView android:id="@+id/content_list" android:layout_height="fill_parent" android:layout_width="fill_parent" android:cacheColorHint="#00000000" android:dividerHeight="0dp" android:divider="@null"> </ListView></LinearLayout>
@Override protected void onResume() { //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); initUI(); //System.out.println("SubpageAppointment.onResume"); super.onResume(); } /** The PAG e_ chem. */ private final int PAGE_CHEM = 1; /** The PAG e_ ray. */ private final int PAGE_RAY = 2; /** The PAG e_ pathology. */ private final int PAGE_PATHOLOGY = 3; /** The PAG e_ mico. */ private final int PAGE_MICO = 4; /** The PAG e_ iagedetail. */ private final int PAGE_IAGEDETAIL = 5; private final int PAGE_CHEMEDTAIL = 6; private final int PAGE_PATHOLOGYDETAIL = 7; /** The m sub page. */ private int mSubPage; 。。。。。。 @Override public void onBackPressed() { if (mSubPage == PAGE_IAGEDETAIL) { mSubPage = PAGE_RAY; initView(); } else if (mSubPage == PAGE_CHEMEDTAIL) { mSubPage = PAGE_CHEM; initView(); } else if (mSubPage == PAGE_PATHOLOGYDETAIL) { mSubPage = PAGE_PATHOLOGY; initView(); } else { super.onBackPressed(); } }<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image_tab" android:layout_height="60dp" android:layout_width="60dp" android:background="@drawable/navi_home" android:layout_centerHorizontal="true"> </ImageView> <TextView android:id="@+id/title_tab" android:layout_width="fill_parent" android:textSize="12dp" android:text="ddffd" android:textColor="#ffFFFFFF" android:textStyle="bold" android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/image_tab" android:layout_marginTop="-10dp" /></RelativeLayout>
final TabHost tabs = (TabHost) this.findViewById(android.R.id.tabhost); final TabSpec tspec1 = tabs.newTabSpec("Journal [1]"); RelativeLayout tabWidget1 = (RelativeLayout) LayoutInflater.from(this) .inflate(R.layout.tab_widget, null); ImageView imageTab1 = (ImageView) tabWidget1 .findViewById(R.id.image_tab); TextView titleTab1 = (TextView) tabWidget1.findViewById(R.id.title_tab); imageTab1.setImageResource(R.drawable.redactivities); titleTab1.setText("Journal [1]"); tspec1.setIndicator(tabWidget1); tspec1.setContent(new Intent(this, SubPageJournal.class)); tabs.addTab(tspec1);<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#ffffffff" android:layout_weight="1"/> <TabWidget android:id="@android:id/tabs" android:tabStripEnabled="false" android:layout_height="fill_parent" android:layout_width="wrap_content" android:background="#ffDE2852" android:layout_weight="0"/> </LinearLayout></TabHost>
Configuration cfg = getResources().getConfiguration(); boolean hor = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE; if (hor) { TabWidget tw = tabs.getTabWidget(); tw.setOrientation(LinearLayout.VERTICAL); } TabHost tabs; 。。。。。。。。 //0 是tab 标签的id tabs.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (0 != tabs.getCurrentTab()) {//Journal [1]是之前add tab 的时候的标签 tabs.setCurrentTabByTag("Journal [1]"); } else { LocalActivityManager manager = getLocalActivityManager();//SubPageJournal 这个是在add tad 的时候setContent的 activity SubPageJournal subpage = (SubPageJournal) manager .getActivity("Journal [1]");//backMainPage()是自己写的函数,用来changeContent,类似于上面提到的initUI() subpage.backMainPage(); } } });