现在的位置: 首页 > 综合 > 正文

tabhost 垂直布局

2014年02月08日 ⁄ 综合 ⁄ 共 3095字 ⁄ 字号 评论关闭

           
 // 创建一个视图,tabwidget视图
 private View createIndicatorView(Context context, TabHost tabHost, String title) {
  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View tabIndicator = inflater.inflate(R.layout.tab_indicator_vertical, tabHost.getTabWidget(), false);
  final TextView titleView = (TextView) tabIndicator.findViewById(R.id.tv_indicator);
  titleView.setText(title);
  return tabIndicator;
 }

  //oncreate方法            
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tabhost);

//获取tabhost对象
  TabHost tabHost = this.getTabHost();

//获取tabwidget对象,并且设置布局方式为垂直布局
  tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);

//创建并添加tabspec对象到tabhost中

  tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(createIndicatorView(this, tabHost, "拜访工作")).setContent(new Intent(this, BrandActivity.class)));

  tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(createIndicatorView(this, tabHost, "视察工作")).setContent(new Intent(this, BrandActivity.class)));

  tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator(createIndicatorView(this, tabHost, "总结工作")).setContent(new Intent(this, BrandActivity.class)));

    }

 

 

      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="fill_parent"
                              android:layout_height="fill_parent"
                              android:orientation="vertical" >

                   <LinearLayout
                             android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:orientation="vertical" >

                          <TabWidget
                                android:id="@android:id/tabs"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />

                  <FrameLayout
                              android:id="@android:id/tabcontent"
                            android:layout_width="fill_parent"
                              android:layout_height="fill_parent" >
                            </FrameLayout>
                   </LinearLayout>

         </TabHost>

 

 

 

      tab_indicator_vertical.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" >

                         <TextView
                             android:id="@+id/tv_indicator"
                             android:padding="30dp"
                             android:textSize="40sp"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content" />

                       </LinearLayout>

抱歉!评论已关闭.