zoukankan      html  css  js  c++  java
  • Tabhost的使用及其界面布局情况

         Tabhost一般用于对主界面的布局,实现界面之间的切换,如图,其实要实现这种效果,就我所知道的方法有两种一个就是tabhostqu实现,另一种就是RadioButton去实现,但是方法都好像差不多吧,这里我就主要讲讲Tabhost如何实现页面切换效果吧

    页面中的代码,很简单,不多讲,需要注意的是这些控件的id,都是系统内部的

     1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     2             android:id="@android:id/tabhost"
     3             android:layout_width="fill_parent"
     4             android:layout_height="fill_parent" >
     5         <RelativeLayout
     6             android:layout_width="fill_parent"
     7             android:layout_height="fill_parent" >
     8         <FrameLayout
     9             android:id="@android:id/tabcontent"
    10             android:layout_width="fill_parent"
    11             android:layout_height="fill_parent"/>             
    12         <TabWidget
    13             android:layout_marginBottom="-4dip"
    14             android:background="@color/black"
    15             android:id="@android:id/tabs"
    16             android:layout_width="fill_parent"
    17             android:layout_height="wrap_content"
    18             android:layout_alignParentBottom="true"/>         
    19     </RelativeLayout>
    20 </TabHost>

     下面是java的源码

     1     public static TabHost mtabhost;
     2 
     3     //Intent的内容
     4     private Intent homeIntent;
     5     private Intent cartIntent;
     6     private Intent accountIntent;
     7     private Intent moreIntent;
     8     
     9     //设置标签
    10     public final static String HOME="home";
    11     private final static String CART="cart";
    12     private final static String ACCOUNT="account";
    13     private final static String MORE="more";
    14     
    15     public static BadgeView badge;//數字提醒
    16     private TabWidget tabs;
     1     //初始化Intent
     2     private void prepareIntent(){
     3         homeIntent=new Intent(this,HomeActivity.class);
     4         cartIntent=new Intent(this,CartActivity.class);
     5         accountIntent=new Intent(this,AccountActivity.class);
     6         moreIntent=new Intent(this,MoreActivity.class);
     7     } 
     8     
     9     //设置Intent的属性
    10     private void setupIntent(){
    11         this.mtabhost=getTabHost();
    12         TabHost localTabHost=this.mtabhost;
    13         localTabHost.addTab(buildTabSpec(HOME, R.drawable.setbuttonbackground_main, homeIntent));//点击图片切换效果
    14         localTabHost.addTab(buildTabSpec(CART,R.drawable.setbuttonbackground_cart, cartIntent));
    15         localTabHost.addTab(buildTabSpec(ACCOUNT,R.drawable.setbuttonbackground_account, accountIntent));
    16         localTabHost.addTab(buildTabSpec(MORE,R.drawable.setbuttonbackground_more, moreIntent));
    17 
    18         getimg(localTabHost);//控制tanhost里面图片的位置
    19     }
    20     
    21     /**
    22      * 构建Tab页面
    23      * @param tag标记
    24      * @param reslabel标签
    25      * @param resIcon图标
    26      * @param content该标签所有展示的内容
    27      * @return返回一个tab
    28      */
    29     private TabHost.TabSpec buildTabSpec(String tag,int resIcon,final Intent content){
    30         return mtabhost.newTabSpec(tag).setIndicator("",getResources().getDrawable(resIcon)).setContent(content);
    31     }
     1     //將TabHost裡面的icon圖標往下移動一點
     2     public void getimg(TabHost mmtab){
     3         TabWidget tabwidget=mmtab.getTabWidget();
     4         for (int i = 0; i < tabwidget.getChildCount(); i++) {
     5             ImageView img=(ImageView)mmtab.getTabWidget().getChildAt(i).findViewById(android.R.id.icon);
     6             img.setPadding(0, 16, 0, 0);
     7         }
     8     }    
     9     
    10 //    //換背景(TabHost)
    11 //    for (int i = 0; i < mtabhost.getTabWidget().getChildCount(); i++) {
    12 //        mtabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);//使背景色透明
    13 //    }
    14       /**  
    15      * 改变Tabhost里面图片或者文字的大小、位置、背景色等  
    16      * @param tabHost  
    17      */   
    18     private void updateTabStyle(final TabHost mTabHost) {   
    19         TabWidget tabWidget = mTabHost.getTabWidget();   
    20         for (int i = 0; i < tabWidget.getChildCount(); i++) {   
    21             RelativeLayout tabView = (RelativeLayout) mTabHost.getTabWidget().getChildAt(i);   
    22                
    23             TextView text = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);   
    24             text.setTextSize(20);   
    25                
    26             // 文字居中   
    27             RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text.getLayoutParams();   
    28             params.width = RelativeLayout.LayoutParams.MATCH_PARENT;   
    29             params.height = RelativeLayout.LayoutParams.MATCH_PARENT;   
    30             // params.addRule(RelativeLayout.CENTER_IN_PARENT);   
    31             text.setLayoutParams(params);   
    32             text.setGravity(Gravity.CENTER);   
    33                
    34             if (mTabHost.getCurrentTab() == i) {   
    35                 // 选中   
    36                 tabView.setBackgroundResource(R.drawable.bg_tab_selected);   
    37                 text.setTextColor(this.getResources().getColorStateList(android.R.color.black));   
    38             } else {   
    39                 // 未选中   
    40                 tabView.setBackgroundResource(R.drawable.bg_tab_normal);   
    41                 text.setTextColor(this.getResources().getColorStateList(android.R.color.darker_gray));   
    42             }   
    43         }   
    44     }   

    之前用的是Tabhost加RadioButton布局,现在改成直接用tabhost了,在图片点击时切换需要注意android:state_selected="true",之前的是 android:state_checkable="true",所以没有效果。好了就写到这里,时间有点紧,有时间在修正。。

    经验的积累在于平时的点滴、
  • 相关阅读:
    转发 GSLB概要和实现原理
    通过openresty && tengine && nginx 动态添加资源到 html 页面
    Terraform 多云管理工具
    vault key 管理工具
    fabio
    keycloak 了解
    访问交换机的三种方式
    LAN、WAN、WLAN的区别
    浅谈团队贡献分如何分配
    Java程序性能分析工具Java VisualVM(Visual GC)—程序员必备利器
  • 原文地址:https://www.cnblogs.com/yrhua/p/3432856.html
Copyright © 2011-2022 走看看