Android 选项卡的使用
2013-12-02 17:44
525 查看
废话不说,代码如下:
首先创建一个xml(TabHost)文件。
效果如图:
首先创建一个xml(TabHost)文件。
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > tab1与选项卡tab1对应 <LinearLayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"> <TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="我是tab1"/></LinearLayout> tab2与选项卡tab2对应 <LinearLayoutandroid:id="@+id/tab2" android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="我是tab2"/></LinearLayout></TabHost>
import android.app.TabActivity; import android.content.res.Resources; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater layoutInflater = LayoutInflater.from(this); Resources res = getResources(); TabHost host = getTabHost(); TabSpec spec; layoutInflater.inflate(R.layout.tab,host.getTabContentView(),true ); spec=host.newTabSpec("1") .setIndicator("tab1", //选项卡的名称 res.getDrawable(android.R.drawable.star_big_on))//选项卡的ICON,androdi.R 是android自带的 .setContent(R.id.tab1);//设置点击选项卡所显示的内容 host.addTab(spec); //添加选项卡 spec=host.newTabSpec("2") .setIndicator("tab2", //选项卡的名称 res.getDrawable(android.R.drawable.star_big_on))//选项卡的ICON,androdi.R 是android自带的 .setContent(R.id.tab2); //设置点击选项卡所显示的内容 host.addTab(spec); //添加选项卡 host.setCurrentTab(0); //用来设置哪个选项卡是默认选项卡 从0开始 } }
效果如图:
相关文章推荐
- android学习-----使用TabLayout实现Tab选项卡以及遇到的一些问题(二)
- Android使用ViewPager+Fragment实现定制TabHost选项卡效果
- 使用Fragment完成Tab选项卡-Android Fragment应用实战
- android TabActivity(选项卡)的使用!
- Android中选项卡TabHost的基本使用
- Android Material Design:滑动指示选项卡android.support.design.widget.TabLayout的简单使用
- Android UI设计 使用选项卡实现界面切换
- Android中选项卡TabHost的基本使用
- Android Material Design:滑动指示选项卡android.support.design.widget.TabLayout的简单使用
- [置顶] Android开发之TabLayout的使用(实现三种常用的选项卡效果)
- Android中选项卡TabHost的基本使用
- Android自定义控件---TabIndicatord选项卡按钮(可用于ViewPager,使用简单)
- 使用Fragment完成Tab选项卡-Android Fragment应用实战
- android TabHost(选项卡)的使用方法
- android中使用tab选项卡
- Android使用TabLayout实现选项卡
- [Android]使用PagerSlidingTabStrip来实现选项卡切换界面的示例
- 选项卡使用方法二(Android学习随笔十三)
- Android使用TabLayout+Fragment实现顶部选项卡
- Android中选项卡TabHost的基本使用