Android仿新浪微博底栏(FragmentTabHost的使用)

转自:http://blog.csdn.net/chenleicpp/article/details/9393619



主布局:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"   
  5.     android:orientation="vertical">  
  6.     <FrameLayout   
  7.         android:id="@+id/realtabcontent"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="0dip"  
  10.         android:layout_weight="1">  
  11.     </FrameLayout>  
  12.       
  13.     <android.support.v4.app.FragmentTabHost  
  14.         android:id="@android:id/tabhost"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="@drawable/tabbar_background">  
  18.         <TabHost   
  19.             android:id="@android:id/tabs"  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="wrap_content">  
  22.         </TabHost>  
  23.      <span style="white-space:pre"> </span><FrameLayout   
  24.             android:id="@android:id/tabcontent"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content">  
  27.         </FrameLayout>          
  28.     </android.support.v4.app.FragmentTabHost>  
  29. </LinearLayout>  
Indicator的layout

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.     android:gravity="center_horizontal|bottom"  
  10.         android:orientation="vertical">  
  11.   
  12.         <ImageView  
  13.             android:id="@+id/iv_icon"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:src="@drawable/icon_home" />  
  17.   
  18.         <TextView  
  19.             android:id="@+id/tv_icon"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:textColor="#FFFFFFFF"  
  23.             android:textSize="10sp" />  
  24.     </LinearLayout>  
  25.   
  26. </LinearLayout>  
底部按钮的selector

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:drawable="@drawable/tabbar_home_highlighted" android:state_selected="true"></item>  
  4.     <item android:drawable="@drawable/tabbar_home"/>  
  5. </selector>  
按下效果的selector

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:drawable="@drawable/tabbar_slider" android:state_pressed="true"/>  
  4.     <item android:drawable="@drawable/tabbar_slider" android:state_selected="true"/>  
  5. </selector>  
MainActivity

[java]  view plain copy
  1. package cn.cl.tebdemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentActivity;  
  5. import android.support.v4.app.FragmentTabHost;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.ImageView;  
  9. import android.widget.TabHost.TabSpec;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * 仿新浪微博底栏 
  14.  * @author chenleicpp 
  15.  * 
  16.  */  
  17. public class MainActivity extends FragmentActivity {  
  18.   
  19.     private FragmentTabHost mTabHost;  
  20.   
  21.     private Class fragmentArray[] = { Main1Fragment.class, Main2Fragment.class, Main3Fragment.class, Main4Fragment.class, Main5Fragment.class };  
  22.     private int iconArray[] = { R.drawable.icon_home, R.drawable.icon_meassage, R.drawable.icon_selfinfo, R.drawable.icon_square,  
  23.             R.drawable.icon_more };  
  24.     private String titleArray[] = { "首页""消息""个人""广场""更多" };  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         setupView();  
  31.     }  
  32.   
  33.     private void setupView() {  
  34.         mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
  35.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  36.         mTabHost.getTabWidget().setDividerDrawable(null);  
  37.   
  38.         int count = fragmentArray.length;  
  39.   
  40.         for (int i = 0; i < count; i++) {  
  41.             TabSpec tabSpec = mTabHost.newTabSpec(titleArray[i]).setIndicator(getTabItemView(i));  
  42.             mTabHost.addTab(tabSpec, fragmentArray[i], null);  
  43.             mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.home_btn_bg);  
  44.         }  
  45.   
  46.     }  
  47.   
  48.     private View getTabItemView(int index) {  
  49.         LayoutInflater layoutInflater = LayoutInflater.from(this);  
  50.         View view = layoutInflater.inflate(R.layout.view_idle_bottom_navi, null);  
  51.   
  52.         ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon);  
  53.         imageView.setImageResource(iconArray[index]);  
  54.   
  55.         TextView textView = (TextView) view.findViewById(R.id.tv_icon);  
  56.         textView.setText(titleArray[index]);  
  57.   
  58.         return view;  
  59.     }  
  60.   
  61. }  
至此为止,主要代码就写完了,具体的请看demo源码:

源码下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值