选项卡之 ——继承TabActivity

来源:互联网 发布:仿淘客基地源码 编辑:程序博客网 时间:2024/05/05 17:06

上一篇文章的选项卡TabHost是从xml中读取的,另外一种形式是继承android.app.TabActivity 类,不过Android 4.0之后貌似不用这个方法了,不管,先贴代码了,了解下。

public class Tab2Activity extends android.app.TabActivity {    private String[] tabName = new String[] { "首页", "看看", "进入" };//tab页的名称      private int[] tabImg = new int[] { R.drawable.b, R.drawable.c, R.drawable.girl };  //tab页显示的图片    private Class[] classes = new Class[] { TabItem1Activity.class,//每个tab页对应的Activity      TabItem2Activity.class, TabItem3Activity.class };        @Override      protected void onCreate(Bundle savedInstanceState) {          // TODO Auto-generated method stub          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_tab2);          createTable();      }        private void createTable() {          TabHost tabHost = getTabHost();          //动态生成Tab页          for (int i = 0; i < tabName.length; i++) {              TabSpec spec = tabHost.newTabSpec("tab" + i).setIndicator(tabName[i], getResources().getDrawable(tabImg[i])).
setContent(new Intent(this, classes[i]));              tabHost.addTab(spec);              View view = tabHost.getTabWidget().getChildAt(i);              view.getLayoutParams().height = 100;//设置tab页的高度              ((TextView) view.findViewById(android.R.id.title)).setTextSize(12);//显示文字的大小              ((ImageView) view.findViewById(android.R.id.icon)).setPadding(0, -5, 0, 0);//调整图片位置以避免图片和文字重叠          }          tabHost.setCurrentTab(0);      }  }

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">      <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"          android:layout_height="fill_parent">           <FrameLayout android:id="@android:id/tabcontent"             android:layout_width="fill_parent" android:layout_height="fill_parent"                android:paddingBottom="50dp">           </FrameLayout>         <RelativeLayout android:layout_width="fill_parent"              android:layout_height="fill_parent" >                <TabWidget android:id="@android:id/tabs"                android:layout_width="fill_parent"               android:layout_height="wrap_content"  android:layout_alignParentBottom="true"/>           </RelativeLayout>              </TabHost>  </LinearLayout> 




0 0
原创粉丝点击