android TabHost控件
(一)TabHost控件,默认是在顶部显示的
TabHost是盛放Tab按钮和Tab内容的首要容器,
TabWidget(tabs标签)用于选择页面,是指一组包含文本或图标的 ,FrameLayout 用于显示页面的内容,是构成Tab页的容器。
注意: (使用系统自带的id,格式为@android:id/)
TabHost (@android:id/tabhost)
FrameLayout(@android:id/tabcontent),
TabWidget( @android:id/tabs)
(二)TabHost的两种跳转方式
一种是利用Layout:
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(Tab1,getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab1));
一种是利用Intent:
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(Tab1,getResources().getDrawable(R.drawable.icon)).setContent(new Intent(this,OtnerActivity.class)));
Tabhost的activity文件分为两种情况,一种是继承自Activity,一种是继承者TabActivity,二者的不同之处在于tabhost的初始化方式不同,跳转的方式相同。
继承Activity :
setContentView(R.layout.***); //设置上面的xml文件
TabHost tabhost = (TabHost) findViewById(R.id.tabhost);
tabhost.setup(); // 初始化TabHost容器
注意加.setup(),否则会有NullPointer的异常
继承TabActivity 类,
TabHost tabHost = getTabHost();//获取当前TabHost对象
LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true); //设置使用tabhost布局
添加Tab分页标签(添加选项卡及设置选项的标题及内容 我们知道添加选项卡需要指定一个TabSpec对象,通过TabHost的newTabSpec(选项卡的标识)可以得到,)
tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.a1)) .setContent(R.id.view1));
所谓TabSpec就是在Tabwidget中显示的那一个个的小格子,addTab(TabSpec)就是增加一个小格子。
TabSpec主要的方法就是setContent()和setIndicator(),设置的参数不同,设置的内容不同,(详解见上面跳转方式的两个例子)
现象: 在Tabhsot中使用intent去打开一个界面是给 Tabhsot.Tabspec页通过setcontent(intent)方法实现跳转
用intent携带数据只能使用一次
若使用多次, 跳转到得页面都只能拿到第一次设置的数据内容。
原因:在Tabhsot.Tabspec的setcontent方法中将intent给final了。
(三)一个典型的TabHost的例子:
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost" android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnalogClock
android:id="@+id/AnalogClock03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" />
<DigitalClock
android:id="@+id/DigitalClock01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:background="#000000" />
</FrameLayout>
</LinearLayout>
</TabHost>
Java代码:
public class TabDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("演示标签分页〉");
//获取TabHost对象
TabHost tabHost=(TabHost) this.findViewById(R.id.tabhost);
tabHost.setup();
//新建一个newTabSpec,设置标签和图标(setIndicator),设置内容(setContent)
TabHost.TabSpec spec=tabHost.newTabSpec("clockTab");
spec.setContent(R.id.AnalogClock03);
spec.setIndicator("模拟时钟", getResources().getDrawable(android.R.drawable.ic_btn_speak_now));
tabHost.addTab(spec);
spec=tabHost.newTabSpec("buttonTab");
spec.setContent(R.id.DigitalClock01);
spec.setIndicator("数字时钟",getResources().getDrawable(android.R.drawable.btn_star_big_on));
tabHost.addTab(spec);
// 设置TabHost的背景颜色
tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
// 设置TabHost的背景图片资源
// tabHost.setBackgroundResource(R.drawable.bg);
// 设置当前现实哪一个标签 tabHost.setCurrentTab(0);
// 0为标签ID
// 标签切换处理,用setOnTabChangedListener
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Toast.makeText(TabDemoActivity.this, "This is a Test!",
Toast.LENGTH_LONG).show();
}
});
} }
(四)android实现底部TabHost
在TabWidget控件中,通过设置android:layout_alignParentBottom="true"属性实现底部TabHost
(五)TabHost改进
如果不希望默认加载事选中一项,而是做成新浪微博底部控制栏的风格,则需要给TabWidget控件添加属性android:visibility="gone",让后给TabWidget添加若干RadioButton子控件,然后将RadioButton设置成自己想要的样式即可。
参考网址:
http://blog.sina.com.cn/s/blog_8373f9b501018b45.html
http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html
android TabHost控件的更多相关文章
- Android TabHost控件 右侧留空并增加按钮
涉及公司内部程序,部分地方进行模糊处理. 公司Android程序的一个子程序UI要进行改版,最初的UI添加按钮是在内容区,而且TabHost空间是正常的标题平均分布.如下图(其实这是改版的第一版,没有 ...
- 一个Demo让你掌握Android所有控件
原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士" 下面给出实现各个组件的源代码: 1.下拉框实现--Spinner packag ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- Android基本控件之Menus
在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...
- Android:控件布局(相对布局)RelativeLayout
RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...
- Android:控件布局(线性布局)LinearLayout
LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...
- 矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
- Android给控件添加触摸回调
Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...
随机推荐
- swift protocol 与类继承结合时的bug
protocol CommonTrait: class { func commonBehavior() -> String } extension CommonTrait { func comm ...
- dwarfdump --arch=arm64 --lookup
解析友盟错误信息重要指令: dwarfdump --arch=arm64 --lookup 0x1001edbc4 /Users/zhoujunbo/Library/Developer/Xcode/A ...
- CAD与用户交互在图面上选择一个实体(com接口VB语言)
主要用到函数说明: IMxDrawUtility::GetEntity 与用户交互到在图面上选择一个实体,详细说明如下: 参数 说明 [out] IMxDrawPoint** pPickPoint 返 ...
- yii 在lnmp下访问问题
lnmp大坑 /usr/local/nginx/conf/fastcgi.conf 文件里面
- csr_matrix参数解析
压缩稀疏矩阵构造时的参数从官网看不明白,参考如下: >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.arr ...
- swift--字符串替换/过滤/切割
//替换 var ReplaceString = "http://www.aimonkey.cn"; var FilterReplace = ReplaceString.strin ...
- 洛谷——P2846 [USACO08NOV]光开关Light Switching
P2846 [USACO08NOV]光开关Light Switching 题目大意: 灯是由高科技——外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右 ...
- <MyBatis>入门一 HelloWorld
1.HelloWorld 导入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependen ...
- 编写函数,第一个参数指定今天是星期几(1 ~ 7),第二个参数指定天数n,返回n天后是星期几
def week(today, n): s = n % 7 + today return "n天后是星期:{}".format(s) print(week(1, 3))
- Tree(树的还原以及树的dfs遍历)
紫书:P155 uva 548 You are to determine the value of the leaf node in a given binary tree that is th ...