关于(TabHost),(Button配合Fragment),(Menu)这三种常见的切换界面手法分析...

前言

作为一个读者变成一个写技术文章的作者,我决定坚持还是写一些简单实用的文章(毕竟理论太高深,也许看的人很蛋疼。。。。我也相信如果有兴趣深入研究,必然会自己找百度的),好了言归真转。

主题内容

在我们的Android开发旅途中,我们不可避免的要处理一种情况就是:在同一个Activity界面里,切换不同的界面。那常用的手法有哪些呢?我总结一下大致分为三种:
[1] TabHost,[2]Button配合Fragment,[3]Menu。下面我将逐一的介绍一下相关用法

TabHost

关于TabHost很多朋友知道,但是却不怎么了解。
盛放Tab的容器就是TabHost。TabHost的实现有两种方式:

第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

让我们一起看看继承TabActivity的写法:
先看main.xml

<?xml version="1.0" encoding="utf-8"?>
 <!-- 定义TabHost组件 -->
 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
   <!-- 定义第一个标签页的内容 -->
   <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <!-- 定义两个TextView用于显示标签页中的内容 -->
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="张老师"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="李老师"/>
   </LinearLayout>
   <!-- 定义第二个标签页的内容 -->
   <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="陈老师"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="游老师"/>
   </LinearLayout>
 </TabHost>

再看Activity的写法

 public class MyActivity extends TabActivity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 
     //调用TabActivity的getTabHost()方法获取TabHost对象
     TabHost tabHost = getTabHost();
 
     //设置使用TabHost布局
     LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
 
     //添加第一个标签页
     tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("数学老师").setContent(R.id.tab01));
 
     //添加第二个标签页,并在其标签上添加一个图片
     tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("语文老师",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
   }
 }

还有不继承TabActivity的写法
继承普通Activity<TabWidget>标签id必须为tabs<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。
先看main.xml

<?xml version="1.0" encoding="utf-8"?>  
  <!-- TabHost必须包含一个 TabWidget和一个FrameLayout-->   
  <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content">  
        <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
            <!-- TabWidget的id属性必须为 @android:id/tabs-->              
            <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent"  
              android:layout_height="wrap_content" />  
            <!-- FrameLayout的id属性必须为 @android:id/tabcontent-->  
             <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">  
                 <!-- 定义第一个标签页的内容 -->
               <LinearLayout android:id="@+id/tab01" android:orientation="vertical"                        android:layout_width="fill_parent" android:layout_height="fill_parent">
                 <!-- 定义两个TextView用于显示标签页中的内容 -->
                 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="张老师"/> 
                 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="李老师"/>
               </LinearLayout>
               <!-- 定义第二个标签页的内容 -->
               <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
                 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="陈老师"/> 
                 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="游老师"/>
               </LinearLayout>
             </FrameLayout>  
         </LinearLayout>  
    </TabHost>  
</LinearLayout>  

再看Activity的写法

public class TabHostTest extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        // 获取TabHost对象  
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        // 如果没有继承TabActivity时,通过该种方法加载启动tabHost(!!!!!!!!!!!!!!!!!!!)  
        tabHost.setup();  
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("数学老师",  
                getResources().getDrawable(R.drawable.icon)).setContent(  
                R.id.view1));  
  
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("语文老师")  
                .setContent(R.id.view2));  
    }  
} 

如果我们需要对TabHost的点击事件进行监听处理,我们可以这样

mTabHost.setOnTabChangedListener(new MyTabChangedListener());
    private class MyTabChangedListener implements TabHost.OnTabChangeListener {
        @Override
        public void onTabChanged(String tabId) {
            //根据自身的业务需求处理相应的操作
        }
    }

以上就是TabHost的基本用法

Button配合Fragment

这个方式我相信广大IT朋友再熟悉不过了,直接上代码
先看main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >
 
    <fragment
        android:id="@+id/math_fragment"
        android:name="com.itfanr.fragmenttab.MathFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10" />
 
    <fragment
        android:id="@+id/chinese_fragment"
        android:name="com.itfanr.fragmenttab.ChineseFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10" />
 
    <RadioGroup
        android:id="@+id/bottomRg"
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:layout_weight="0.5"
        android:orientation="horizontal" 
        android:gravity="center">
 
        <RadioButton
            android:id="@+id/rbOne"
            android:background="@drawable/ofm_collect_icon"
            android:checked="true"
            android:text="数学老师" />
 
        <RadioButton
            android:id="@+id/rbTwo"
            android:background="@drawable/ofm_collect_icon"
            android:text="语文老师" />
    </RadioGroup>
 
</LinearLayout>

再看Activity的写法

public class MainActivity extends Activity {
 
    private Fragment[] mFragments;  
    private RadioGroup bottomRg;  
    private FragmentManager fragmentManager;  
    private FragmentTransaction fragmentTransaction;  
    private RadioButton rbOne, rbTwo; 
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         mFragments = new Fragment[2];  
            fragmentManager = this.getFragmentManager();  
            mFragments[0] = fragmentManager.findFragmentById(R.id.math_fragment);  
            mFragments[1] = fragmentManager.findFragmentById(R.id.chiese_fragment);  
            fragmentTransaction = fragmentManager.beginTransaction()  
                    .hide(mFragments[0]).hide(mFragments[1]);  
            fragmentTransaction.show(mFragments[0]).commit(); 
            setFragmentIndicator() ;
    }
     
     private void setFragmentIndicator() {  
           
            this.bottomRg = (RadioGroup) findViewById(R.id.bottomRg);  
            this.rbOne = (RadioButton) findViewById(R.id.rbOne);  
            this.rbTwo = (RadioButton) findViewById(R.id.rbTwo);  
       
            bottomRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
       
                @Override  
                public void onCheckedChanged(RadioGroup group, int checkedId) {  
                    fragmentTransaction = fragmentManager.beginTransaction()  
                            .hide(mFragments[0]).hide(mFragments[1]);  
                    switch (checkedId) {  
                    case R.id.rbOne:  
                        fragmentTransaction.show(mFragments[0]).commit();  
                        break;         
                    case R.id.rbTwo:  
                        fragmentTransaction.show(mFragments[1]).commit();  
                        break;         
                    default:  
                        break;  
                    }  
                }  
            });  
        } 

以上是Button配合Fragment的写法,小小的拓展一下知识点:关于Activity跟Fragment的生命周期的关系区别有兴趣可以点击http://blog.csdn.net/zjclugger/article/details/10442335查看详情

Menu

这里我就讲解一下代码设置Menu的做法,XML的做法比较简单就不一一说明了
直接在Activity内部完成操作

    //重写菜单事件
    public boolean onCreateOptionsMenu(Menu menu) {  
            menu.add(0, 1, 1, "数学老师"); 
            menu.add(0, 2, 2, "语文老师");            
            return super.onCreateOptionsMenu(menu);  
        } 
    //给对应菜单选项注册点击事件
    public boolean onOptionsItemSelected(MenuItem item) {  
        switch (item.getItemId()) {  
            case 1:  
                //显示数学老师的内容跟button的显示方式类似
                break;  
            case 2:  
                //显示语文老师的内容跟button的显示方式类似
               break;
            default:  
               break;  
            }  
            return super.onOptionsItemSelected(item);  
        }        

总结

关于这三种切换界面的基本手法的分析,我已经介绍完了,然后我们再分析一下什么时候用什么手法比较合适。如果你对按钮的显示效果有要求(如不同的情形显示不同的按钮展示效果),推荐第二种。如果是按钮比较少的时候,操作又想比较简单,推荐TabHost。如果按钮很多,却又不想显示在界面上的,那Menu的手法就是不二的首选。感谢大家的阅读,有需要的朋友欢迎收藏,哈哈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值