>>分享Android开发相关的技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 19957 个阅读者 刷新本主题
 * 贴子主题:  Android UI学习 - Tab的学习和使用 回复文章 点赞(0)  收藏  
作者:Jacky    发表时间:2020-03-31 20:15:39     消息  查看  搜索  好友  邮件  复制  引用

     本文是参考Android官方提供的sample里面的ApiDemos的学习总结。          

  TabActivity

   首先Android里面有个名为TabActivity来给我们方便使用。其中有以下可以关注的函数:

     public TabHost getTabHost ()  获得当前TabActivity的TabHost

   public TabWidget getTabWidget () 获得当前TabActivity的TabWidget

   public void setDefaultTab (String tag) 这两个函数很易懂,就是设置默认的Tab

     public void setDefaultTab (int index)  通过tab名——tag或者index(从0开始)

   protected void onRestoreInstanceState (Bundle state) 这两个函数的介绍可以

   protected void onSaveInstanceState (Bundle outState) 参考 Activity的生命周期        

  TabHost

   那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。

   现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。

  TabHost类的一些函数:

   public void addTab (TabHost.TabSpec tabSpec) 添加tab,参数TabHost.TabSpec通过下面的函数返回得到

   public TabHost.TabSpec newTabSpec (String tag) 创建TabHost.TabSpec

   public void clearAllTabs () remove所有的Tabs

   public int getCurrentTab ()

   public String getCurrentTabTag ()

   public View getCurrentTabView ()

   public View getCurrentView ()

   public FrameLayout getTabContentView () 返回Tab content的FrameLayout

     public TabWidget getTabWidget ()

   public void setCurrentTab (int index)       设置当前的Tab by index

   public void setCurrentTabByTag (String tag) 设置当前的Tab by tag

   public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理

   public void setup () 这个函数后面介绍    

  TabHost.TabSpec

   从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。

   而要设置tab的label和content,需要设置TabHost.TabSpec类。 引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西:

   public String getTag ()

   public TabHost.TabSpec setContent

   public TabHost.TabSpec setIndicator

   我理解这里的 Indicator就是Tab上的label,它可以

   设置label setIndicator (CharSequence label)

   或者同时 设置label和icon setIndicator (CharSequence label, Drawable icon)

   或者直接 指定某个view setIndicator (View view)

   对于 Content,就是Tab里面的内容,可以

   设置View的id setContent(int viewId)

   或者 TabHost.TabContentFactory的createTabContent(String tag)来处理: setContent(TabHost.TabContentFactory contentFactory)

   或者用 new Intent来引入其他Activity的内容: setContent(Intent intent)

   现在来看官方的Views/Tabs/Content By Id例子:

     点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

  代码

  1.    public   class  Tabs1  extends TabActivity {
  2.        @Override  
  3.        protected   void  onCreate(Bundle savedInstanceState) {
  4. super .onCreate(savedInstanceState);
  5. TabHost tabHost = getTabHost();
  6. LayoutInflater.from( this).inflate (R.layout.tabs1, tabHost.getTabContentView(),  true );
  7.           tabHost.addTab(tabHost.newTabSpec( "tab1" )
  8.        .setIndicator( "tab1" )
  9.        .setContent(R.id.view1));
  10.           tabHost.addTab(tabHost.newTabSpec( "tab3" )
  11.        .setIndicator( "tab2" )
  12.        .setContent(R.id.view2));
  13.           tabHost.addTab(tabHost.newTabSpec( "tab3" )
  14.        .setIndicator( "tab3" )
  15.        .setContent(R.id.view3));
  16.       }
  17.   }

    原来在获取TabHost后,需要用LayoutInflater来得到Layout,LayoutInflater在后面就详细介绍。R.layout.tabs1的内容:

  1.    < FrameLayout   xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.        android:layout_width = "fill_parent"  
  3.        android:layout_height = "fill_parent" >  
  4.        < TextView   android:id = "@+id/view1"  
  5. android:background = "@drawable/blue"  
  6. android:layout_width = "fill_parent"  
  7. android:layout_height = "fill_parent"  
  8. android:text = "@string/tabs_1_tab_1" />  
  9.        < TextView   android:id = "@+id/view2"  
  10. android:background = "@drawable/red"  
  11. android:layout_width = "fill_parent"  
  12. android:layout_height = "fill_parent"  
  13. android:text = "@string/tabs_1_tab_2" />  
  14.        < TextView   android:id = "@+id/view3"  
  15. android:background = "@drawable/green"  
  16. android:layout_width = "fill_parent"  
  17. android:layout_height = "fill_parent"  
  18. android:text = "@string/tabs_1_tab_3" />  
  19.    </ FrameLayout >  
  20.   <! -- strings.xml
  21.        < string   name = "tabs_1_tab_1" > tab1 </ string >  
  22.        < string   name = "tabs_1_tab_2" > tab2 </ string >  
  23.        < string   name = "tabs_1_tab_3" > tab3 </ string >  
  24.   -- >  

原来是用FrameLayout的!

   而让Tab1的内容显示tab1且背景为Blue,是setContent(R.id.view1)这里引用了TextView1。现在就基本明白如何添加tab以及如何设置label和content了。

  接下来看看Views/Tabs/Content By Factory的例子:

     点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

    代码

  1.    public   class  Tabs2  extends TabActivity   implements TabHost.TabContentFactory  {
  2.        @Override  
  3.        protected   void  onCreate(Bundle savedInstanceState) {
  4. super .onCreate(savedInstanceState);
  5. final  TabHost tabHost = getTabHost();
  6.           tabHost.addTab(tabHost.newTabSpec( "tab1" )
  7.        .setIndicator( "tab1" , getResources().getDrawable(R.drawable.star_big_on))
  8.        . setContent( this) );
  9.           tabHost.addTab(tabHost.newTabSpec( "tab2" )
  10.        .setIndicator( "tab2" )
  11.        .setContent( this ));
  12.           tabHost.addTab(tabHost.newTabSpec( "tab3" )
  13.        .setIndicator( "tab3" )
  14.        .setContent( this ));
  15.       }
  16.        public  View  createTabContent(String tag) {
  17. final  TextView tv =  new  TextView( this );
  18.           tv.setText( "Content for tab with tag "  + tag);
  19. return  tv;
  20.       }
  21.   }

     可以看到通过override重写(重新实现)父类TabHost.TabContentFactory中的方法View createTabContent(String tag)来实现不同tab的不同content。同时在setContent的参数设置为相应的TabContentFactory。

     原来createTabContent是在每个tab第一次显示时才调用的,随后再次显示该tab就不会再次调用的,我自己用Logcat查看到的!这一点很关键,就是说在createTabContent是在tab没有完全创建前调用的,这意味在createTabContent里面是不能调用getCurrentTabView等之类的函数的,否则就出错!

   至于Views/Tabs/Content By Intent例子,就只是贴出代码,不给截图了:

  1.    public   class  Tabs3  extends TabActivity {
  2.        @Override  
  3.        protected   void  onCreate(Bundle savedInstanceState) {
  4. super .onCreate(savedInstanceState);
  5. final  TabHost tabHost = getTabHost();
  6.           tabHost.addTab(tabHost.newTabSpec( "tab1" )
  7.        .setIndicator( "list" )
  8.        .setContent(  new Intent( this, List1. class) ));
  9.           tabHost.addTab(tabHost.newTabSpec( "tab2" )
  10.        .setIndicator( "photo list" )
  11.        .setContent(  new Intent( this, List8. class)) );
  12. // This tab sets the intent flag so that it is recreated each time  
  13. // the tab is clicked.  
  14.           tabHost.addTab(tabHost.newTabSpec( "tab3" )
  15.        .setIndicator( "destroy" )
  16.        .setContent( new  Intent( this , Controls2. class )
  17.     .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
  18.       }
  19.   }

效果:Tab1的内容是List1的Activity,Tab2的是List8的Activity,Tab3的是controls2.Activity。        

  TabHost.OnTabChangeListener

    TabHost.OnTabChangeListener接口只有一个抽象方法onTabChanged(String tagString),明显地,在 onTabChanged(String tagString)方法里面swtich..case..来判断tagString分别处理就行了。        

  TabHost.setup()

   在此贴出SDK doc里面的相关解释:

public void setup ()         Since: API Level 1

Call setup() before adding tabs if loading TabHost using findViewById(). However, You do not need to call setup() after getTabHost() in TabActivity. Example:

      mTabHost = (TabHost)findViewById(R.id.tabhost);

       mTabHost.setup();

       mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");

     //我的理解是,如果要用到findViewById来获取TabHost,然后add tabs的话,需要在addTab前call setup();

     public void setup (LocalActivityManager activityGroup)         Since: API Level 1

          If you are using  setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. <u>This is done for you if you extend TabActivity.</u>

     Parameters

     activityGroup Used to launch activities for tab content.

        

----------------------------
原文链接:https://blog.51cto.com/android/315208

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-04-08 09:25:24 重新编辑]
  Java面向对象编程-->数据类型
  JavaWeb开发-->Web运作原理(Ⅳ)
  JSP与Hibernate开发-->Spring、JPA与Hibernate的整合
  Java网络编程-->通过JDBC API访问数据库
  精通Spring-->通过Vuex进行状态管理
  Vue3开发-->虚拟DOM和render()函数
  android异步更新UI
  Android内核开发:图解Android系统的启动过程
  Android多线程及异步处理问题
  Android多屏幕适配
  在Window中下载Android源代码的步骤
  Android开发: 文件读写
  Android在SDcard建文件夹(在Android中移动文件必用)
  Android HelloGallery范例实验记录
  Android 使用SQLite数据库
  Android开发实践:Android.mk模板
  Android性能优化之视图篇(渲染机制)_移动开发_Applicaton的...
  android 系统自带的卡帧警告
  下拉框+Switch开关控件
  Android 启动页倒计时自定义view实现
  Android 判断当前设备是手机还是平板
  更多...
 IPIP: 已设置保密
楼主      
1页 2条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。