实现、设置-Android TabWidget-by小雨
查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!
首先先看一个小例子,接着讲授理原
- TabTest.java
- view plaincopy to clipboardprint?
- package org.hualang.tab;
- import android.app.Activity;
- import android.app.TabActivity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.widget.TabHost;
- import android.widget.Toast;
- import android.widget.TabHost.OnTabChangeListener;
- public class TabTest extends TabActivity {
- /** Called when the activity is first created. */
- TabHost tabhost;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //获得TabHost对象
- tabhost = getTabHost();
- //为TabHost添加签标
- //新建一个newTabSpec(newTabSpec)
- //设置其签标和标图(setIndicator)
- //设置容内(setContent)
- tabhost.addTab(tabhost.newTabSpec("tab1")
- .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
- .setContent(R.id.text1));
- tabhost.addTab(tabhost.newTabSpec("tab2")
- .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
- .setContent(R.id.text2));
- tabhost.addTab(tabhost.newTabSpec("tab3")
- .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
- .setContent(R.id.text3));
- //设置TabHost的背景颜色
- //tabhost.setBackgroundColor(Color.argb(150,22,70,150));
- //设置TabHost的背景图片资源
- tabhost.setBackgroundResource(R.drawable.bg0);
- //设置前当示显哪个签标
- tabhost.setCurrentTab(0);
- //签标换切事件处理,setOnTabChangedListener
- tabhost.setOnTabChangedListener(new OnTabChangeListener()
- {
- public void onTabChanged(String tabId)
- {
- Toast toast=Toast.makeText(getApplicationContext(), "现在是"+tabId+"签标", Toast.LENGTH_SHORT);
- toast.show();
- }
- });
- }
- }
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">
- <LinearLayout
- android:orientation="vertical"
- 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" />
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/text1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="选项卡1" />
- <TextView
- android:id="@+id/text2"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="选项卡2" />
- <TextView
- android:id="@+id/text3"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="选项卡3" />
- </FrameLayout>
- </LinearLayout>
- </TabHost>
Android TabWidget的实现可以分为二种,一种是应用准标TabActivity实现,另外一种可以自定义方法实现,种这方法实现起来对相较比复杂,但对于要实现较比多元化的view是很好的,这里我们简略看下源码
一、通用做法
继承TabActivity,实现自己的TabActivity
- import android.app.Activity;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TabHost;
- import android.widget.TabHost.OnTabChangeListener;
- public class TabWidgetDemo2 extends TabActivity implements OnTabChangeListener {
- private TabHost mTabHost;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabwidgetdemo2);
- mTabHost = getTabHost();
- mTabHost.setOnTabChangedListener(this);
- setupTab1();
- setupTab2();
- mTabHost.setCurrentTab(1);
- }
- private void setupTab2() {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_MAIN);
- intent.setClass(this, TabWidget2.class);
- mTabHost.addTab(mTabHost.newTabSpec("TabWidget2")
- .setIndicator("TabWidget2",getResources().getDrawable(R.drawable.icon))
- .setContent(intent));
- }
- private void setupTab1() {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_MAIN);
- intent.setClass(this, TabWidget1.class);
- mTabHost.addTab(mTabHost.newTabSpec("TabWidget1")
- .setIndicator("TabWidget1",getResources().getDrawable(R.drawable.icon))
- .setContent(intent));
- }
- public void onTabChanged(String tabId) {
- // TODO Auto-generated method stub
- Activity activity = getLocalActivityManager().getActivity(tabId);
- if (activity != null) {
- activity.onWindowFocusChanged(true);
- }
- }
- }
二个tab对应的Activity,先看TabWidget1,这个类在第二种实现中还会用到,因此我们可以看到对Action的判断。
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import com.android.exampledemo.R;
- import com.android.exampledemo.util.DemoUtils;
- public class TabWidget1 extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- Intent intent = this.getIntent();
- if (intent.getAction().equals(Intent.ACTION_MAIN)){
- setContentView(R.layout.tabwidgetdemo2_1);
- }
- else {
- setContentView(R.layout.tabwidget_1);
- DemoUtils.updateButtonBar((Activity)this,R.id.contactstab);
- }
- }
- }
再看一下TabWidget2,这个Activity我们在第二种实现方法中也会用到。
- import com.android.exampledemo.R;
- import com.android.exampledemo.util.DemoUtils;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- public class TabWidget2 extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- Intent intent = this.getIntent();
- if (intent.getAction().equals(Intent.ACTION_MAIN)){
- setContentView(R.layout.tabwidgetdemo2_1);
- }
- else {
- setContentView(R.layout.tabwidget_2);
- DemoUtils.updateButtonBar((Activity)this,R.id.groupstab);
- }
- }
- }
最后就是各个Activity对应的layout
1.tabwidgetdemo2.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">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TabWidget android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="68dip"
- android:paddingLeft="1dip"
- android:paddingRight="1dip"
- android:paddingTop="4dip"
- />
- <FrameLayout android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- />
- </LinearLayout>
- </TabHost>
2.二个sub tab对应的layout
- Layout1
- <?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:background="#FFF">
- <TextView android:id="@+id/textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tab Widget first">
- </TextView>
- </LinearLayout>
- Layout2
- <?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:background="#FFF">
- <TextView android:id="@+id/textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tab Widget second">
- </TextView>
- </LinearLayout>
方法2:
先创立一个Activity (TabWidgetDemo)
- 1.TabWidgetDemo.java
- import com.android.exampledemo.R;
- import com.android.exampledemo.util.DemoUtils;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- //not use tabhost to organized
- public class TabWidgetDemo extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- //int activeTab = DemoUtils.getIntPref(this, "activetab", R.id.artisttab);
- SharedPreferences prefs =
- getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
- int activeTab = prefs.getInt("activetab", R.id.contactstab);
- if (activeTab != R.id.contactstab
- && activeTab != R.id.groupstab) {
- activeTab = R.id.contactstab;
- }
- DemoUtils.activateTab(this, activeTab);
- }
- }
- 2.DemoUtils
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.view.View;
- import android.widget.TabWidget;
- import com.android.exampledemo.R;
- public class DemoUtils {
- static int sActiveTabIndex = -1;
- public static void activateTab(Activity a,int active_id){
- Intent intent = new Intent(Intent.ACTION_PICK);
- switch (active_id) {
- case R.id.contactstab:
- intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/tb_contacts");
- break;
- case R.id.groupstab:
- intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/tb_groups");
- break;
- default:
- return;
- }
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- a.startActivity(intent);
- a.finish();
- a.overridePendingTransition(0,0);
- }
- public static void updateButtonBar(Activity a, int highlight) {
- final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);
- for (int i = ll.getChildCount() - 1; i >= 0; i--) {
- View v = ll.getChildAt(i);
- boolean isActive = (v.getId() == highlight);
- if (isActive) {
- ll.setCurrentTab(i);
- sActiveTabIndex = i;
- }
- v.setTag(i);
- v.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- int id = v.getId();
- if (id == ll.getChildAt(sActiveTabIndex).getId()) {
- return;
- }
- activateTab((Activity)ll.getContext(),id );
- ll.setCurrentTab((Integer) v.getTag());
- }});
- }
- }
- }
二个Tab sub activity前一方法中经已给出,这里我们只需要看一下layout的实现就能够了
1>buttonbar.xml
- <?xml version="1.0" encoding="utf-8"?>
- <TabWidget xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/buttonbar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/contactstab"
- android:focusable="true"
- android:drawableTop="@drawable/icon"
- android:background="@drawable/buttonbarbackground"
- android:text="Contacts"
- android:textColor="@color/tab_indicator_text"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:paddingTop="7dip"
- android:paddingBottom="2dip"
- android:gravity="center"
- android:layout_weight="1"
- android:layout_marginLeft="-3dip"
- android:layout_marginRight="-3dip"
- android:layout_width="match_parent"
- android:layout_height="84dip"
- android:singleLine="true"
- android:ellipsize="marquee" />
- <TextView
- android:id="@+id/groupstab"
- android:focusable="true"
- android:drawableTop="@drawable/icon"
- android:background="@drawable/buttonbarbackground"
- android:text="Group"
- android:textColor="@color/tab_indicator_text"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:paddingTop="7dip"
- android:paddingBottom="2dip"
- android:gravity="center"
- android:layout_weight="1"
- android:layout_marginLeft="-3dip"
- android:layout_marginRight="-3dip"
- android:layout_width="match_parent"
- android:layout_height="84dip"
- android:singleLine="true"
- android:ellipsize="marquee" />
- </TabWidget>
2>tabwidget_1.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">
- <include layout="@layout/battonbar" />
- <ExpandableListView android:id="@+id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:footerDividersEnabled="true"
- android:fadeScrollbars="true"
- android:drawSelectorOnTop="true">
- </ExpandableListView>
- </LinearLayout>
3> tabwidget_2.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">
- <include layout="@layout/battonbar" />
- </LinearLayout>
文章结束给大家分享下程序员的一些笑话语录: 真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。
转载于:https://www.cnblogs.com/jiangu66/archive/2013/04/14/3020375.html
实现、设置-Android TabWidget-by小雨相关推荐
- Android TabWidget
首先先看一个小例子,接着讲解原理 TabTest.java view plaincopy to clipboardprint? package org.hualang.tab; import andr ...
- android 固定底部导航,如何设置android底部导航栏位置固定在android
请帮我设置底部导航栏位置固定在底部, ,因为我在输入editText字段时遇到问题,底部导航栏向上移动并覆盖其他领域如何设置android底部导航栏位置固定在android 代码: xmlns:and ...
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
在进行UI布局的时候,可能常常会用到 android:gravity 和 android:layout_Gravity 这两个属性. 关于这两个属性的差别,网上已经有许多人进行了说明,这边再简单说一 ...
- android重新编译res,使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中lt;meta-datagt;变量的值...
你也能够查看我的其它同类文章.也会让你有一定的收货 关于使用Gradle来控制版本号和生成不同版本号的代码.我总结了三篇文章,网上关于这些知识,都比較零散.我在学习这些的之前.根本不知道还有这种方法. ...
- (转)Android属性设置android:noHistory=true
设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...
- GridView xml中设置android:focusable=false无效的原因
最近Tv项目中有个小问题,需要gridview展示内容,但是不可获取焦点,于是xml中设置android:focusable="false",设想会成功,但是实际操作,发现还是可以 ...
- 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中lt;meta-datagt;变量的值...
转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49247815 文章出自:薛瑄的博客 你也能够查看我的其它同类文章.也会让你有一定的 ...
- android gradle 设置,android gradle配置指南
Gradle简介 Gradle 是一个基于Ant和Maven概念的项目自动化建构工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,这比我们的ANT使用XML构建配置要灵活的多.在 ...
- android ctrl 左键鼠标左键直接打开xml文件夹,设置Android Studio通过Ctrl+左键查看源码...
开始学习android的时候希望能点进系统提供的控件中查看源码,但是实际操作发现,看到的每个源文件方法都是抛出的Exception,所以想要设置成可以直接查看具体实现,记录下我自己的操作方法. 1.首 ...
最新文章
- [POI2009]KAM-Pebbles BZOJ1115 [ 待填坑 ] 博弈
- 我把帮带份饭的信息错发给导师后.......
- Windows 如何在cmd命令行中查看、修改、删除与添加环境变量
- 用aspx开发html5页面,ASP.NET使aspx页面能接受HTML,asp的页面传送的文件-.NET教程,Asp.Net开发...
- pyspark报错java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
- mysql8.0提示命令_Mysql 8.0 相关命令
- CF 1475 D. Cleaning the Phone 思维模型
- 操作系统学习笔记-2.1.1.进程的定义、组成、组织方式、特征
- mysql连接数据库的包_java连接mysql数据库包
- Sqoop导入数据发生数据倾斜问题 及更好解决
- 在Sql Server 2008上安装SDE 9.3
- 共轭函数Fenchel不等式
- SAP在阿里云白皮书-第二章 阿里云概念解析
- matlab自动运行,在指定时间自动运行Matlab程序
- HTML——选择器(1)
- linux远程登录maridb,linux – 无法使用phpMyAdmin登录到远程MariaDB服务器,但在shell中工作...
- 2021年R1快开门式压力容器操作考试题及R1快开门式压力容器操作实操考试视频
- 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分解决办法
- 网络设置巨形帧_网卡设置 网卡的高级设置说明
- git 查看自己秘钥_git秘钥配置--转
热门文章
- 前端学习(3280):iterator
- 前端学习(3249):react的文件src
- react学习(21)---接口加回调显示
- 工作372-cookie和sessionstroage以及localstroage区别
- [css] 手动写动画最小时间间隔是多少,为什么?
- [css] 说出至少十条你理解的css规范
- 前端学习(2656):vue2中用v-model实现
- 前端学习(2522):添加博客的样式
- “睡服”面试官系列第六篇之set数据结构(建议收藏学习)
- 前端学习(603):计算机基础