Android——TabWidget(切换卡)
2016-04-29 11:56
513 查看
【注】
1、TabHost的id必须@android:id/tabhost;必须有一个id为@android:id/tabs的TabWidget;必须有一个id为@android:id/tabcontent的FrameLayout。
2、在addTab方法中必须有setIndicator。
3、想要将切换卡放到屏幕下面,需要使用RelativeLayout布局
【介绍】切换卡(TabWidget)是一种相对复杂的继承自LinearLayout的布局管理器,通过多个标签来切换显示不同的内容,一个TabWidget主要由一个TabHost来存放多个Tab标签容器,再在Tab容器中加入其它控件,通过addTab方法添加新的Tab。
【属性】
1、包含线性布局的属性,如orientation和gravity
2、自己的属性,如divider,tabStripEnabled,tabStripLeft,tabStripRight
【示例】
[layout/.xml]
[.java]
【运行】
1、TabHost的id必须@android:id/tabhost;必须有一个id为@android:id/tabs的TabWidget;必须有一个id为@android:id/tabcontent的FrameLayout。
2、在addTab方法中必须有setIndicator。
3、想要将切换卡放到屏幕下面,需要使用RelativeLayout布局
【介绍】切换卡(TabWidget)是一种相对复杂的继承自LinearLayout的布局管理器,通过多个标签来切换显示不同的内容,一个TabWidget主要由一个TabHost来存放多个Tab标签容器,再在Tab容器中加入其它控件,通过addTab方法添加新的Tab。
【属性】
1、包含线性布局的属性,如orientation和gravity
2、自己的属性,如divider,tabStripEnabled,tabStripLeft,tabStripRight
【示例】
[layout/.xml]
<?xml version="1.0"encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabhost"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent"> <TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/textview1" android:text="这是第一个tab页"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/textview2" android:text="这是第二个tab页"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/textview3" android:text="这是第三个tab页"/> </FrameLayout> </LinearLayout> </TabHost><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
[.java]
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost th=(TabHost) findViewById(android.R.id.tabhost); th.setup(); th.addTab(th.newTabSpec("tab1").setIndicator("First").setContent(R.id.textview1)); th.addTab(th.newTabSpec("tab2").setIndicator("Second").setContent(R.id.textview2)); th.addTab(th.newTabSpec("tab3").setIndicator("Third").setContent(R.id.textview3)); } }
【运行】
相关文章推荐
- Android中ListPreference的使用
- Android——布局
- android view 事件传递
- Android布局的优化
- Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案
- Android FragmentManager BackStackRecord.run throwing NullPointerException
- Android实现 UDP
- Android实现RecyclerView自定义列表、点击事件以及下拉刷新
- android listview需要呈现多个布局
- Android中解决EditText放到popupWindow中,原有复制、粘贴、全选、选择功能失效问题
- android viewPager 滑动标题颜色渐变效果
- android 获取文件路径(内置sd卡和外置sd卡)。
- Android Animation
- 反射还能这么玩?
- Android录制屏幕在转出gif
- weakreference handler android莫须有的罪名
- Android 如何获取在strings里面设置的值
- AidlTokenType.import or android:layout_height="match_parent"
- Android涉及到的设计模式
- android中关于上下文菜单ContextMenu数据值的转递