zoukankan      html  css  js  c++  java
  • Android开发之底部导航栏标准

    以下是封装的库源码:

     1 package com.example.oldtab;
     2 
     3 import java.util.ArrayList;
     4 
     5 import android.content.res.Resources;
     6 import android.graphics.drawable.Drawable;
     7 import android.support.v4.app.FragmentTabHost;
     8 import android.support.v7.app.ActionBarActivity;
     9 import android.view.LayoutInflater;
    10 import android.view.View;
    11 import android.widget.TabHost;
    12 import android.widget.TextView;
    13 
    14 public class NsFragmentTabHost implements TabHost.OnTabChangeListener {
    15     private FragmentTabHost fragmentTabHost;
    16     private LayoutInflater layoutInflater;
    17     private Resources resources;
    18     private ArrayList<NsFragmentItem> tabs;
    19     private NsFragmentItem preItem;
    20 
    21     NsFragmentTabHost(FragmentTabHost tabhost, ActionBarActivity activity) {
    22         this(tabhost, activity, android.R.id.tabcontent);
    23     }
    24 
    25     NsFragmentTabHost(FragmentTabHost tabhost, ActionBarActivity activity,
    26             int realContentid) {
    27         fragmentTabHost = tabhost;
    28         tabs = new ArrayList<NsFragmentItem>();
    29         layoutInflater = activity.getLayoutInflater();
    30         resources = activity.getResources();
    31         tabhost.setup(activity.getBaseContext(),
    32                 activity.getSupportFragmentManager(), realContentid);
    33         tabhost.setOnTabChangedListener(this);
    34     }
    35 
    36     public View newTabView(int layoutid, int backgroung, String title) {
    37         TextView tv = (TextView) layoutInflater.inflate(layoutid, null);
    38         tv.setText(title);
    39         Drawable draw = resources.getDrawable(backgroung);
    40         tv.setCompoundDrawablesWithIntrinsicBounds(null, draw, null, null);
    41         return tv;
    42     }
    43 
    44     public void AddTab(NsFragmentItem item) {
    45         tabs.add(item);
    46         fragmentTabHost.addTab(fragmentTabHost.newTabSpec(item.tag)
    47                 .setIndicator(item.view), item.fragment, null);
    48     }
    49 
    50     public NsFragmentItem getTabByTag(String tag) {
    51         for (NsFragmentItem item : tabs) {
    52             if (item.tag == tag) {
    53                 return item;
    54             }
    55         }
    56         return null;
    57     }
    58 
    59     @Override
    60     public void onTabChanged(String tabId) {
    61         NsFragmentItem item = getTabByTag(tabId);
    62         if (item != null) {
    63             if (preItem != null) {
    64                 preItem.view.setBackgroundResource(preItem.upStyle);
    65             }
    66             preItem = item;
    67             item.view.setBackgroundResource(item.downStyle);
    68         }
    69     }
    70 
    71     public static class NsFragmentItem {
    72         public View view;
    73         public String tag;
    74         public Class<?> fragment;
    75         public int downStyle;
    76         public int upStyle;
    77     }
    78 }

    下面就是示例,如何使用上面的库

     1 package com.example.oldtab;
     2 
     3 import android.os.Bundle;
     4 import android.support.v4.app.FragmentTabHost;
     5 import android.support.v7.app.ActionBarActivity;
     6 import android.view.View;
     7 
     8 import com.example.oldtab.NsFragmentTabHost.NsFragmentItem;
     9 
    10 public class Main extends ActionBarActivity {
    11 
    12     private static final String TAG_PERSON = "person";
    13     private static final String TAG_GROUP = "group";
    14     private static final String TAG_SETTING = "setting";
    15 
    16     private NsFragmentTabHost nsTabHost;
    17     private FragmentTabHost tabHost;
    18 
    19     @Override
    20     protected void onCreate(Bundle savedInstanceState) {
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.activity_main);
    23         tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    24         nsTabHost = new NsFragmentTabHost(tabHost, this, R.id.realtabcontent);
    25         View i1 = nsTabHost.newTabView(R.layout.tabitem,
    26                 R.drawable.ic_action_person, "个人");
    27         View i2 = nsTabHost.newTabView(R.layout.tabitem,
    28                 R.drawable.ic_action_group, "集体");
    29         View i3 = nsTabHost.newTabView(R.layout.tabitem,
    30                 R.drawable.ic_action_settings, "设置");
    31         NsFragmentItem item1 = new NsFragmentItem();
    32         item1.view = i1;
    33         item1.tag = TAG_PERSON;
    34         item1.fragment = PersonFragment.class;
    35         item1.downStyle = R.drawable.tab_spec_down;
    36         item1.upStyle = R.drawable.tab_spec_nomal;
    37         NsFragmentItem item2 = new NsFragmentItem();
    38         item2.view = i2;
    39         item2.tag = TAG_GROUP;
    40         item2.fragment = GroupFragment.class;
    41         item2.downStyle = R.drawable.tab_spec_down;
    42         item2.upStyle = R.drawable.tab_spec_nomal;
    43         NsFragmentItem item3 = new NsFragmentItem();
    44         item3.view = i3;
    45         item3.tag = TAG_SETTING;
    46         item3.fragment = SettingFragment.class;
    47         item3.downStyle = R.drawable.tab_spec_down;
    48         item3.upStyle = R.drawable.tab_spec_nomal;
    49         nsTabHost.AddTab(item1);
    50         nsTabHost.AddTab(item2);
    51         nsTabHost.AddTab(item3);
    52     }
    53 }
  • 相关阅读:
    冒泡排序、选择排序、简单二分查找
    asp.net和js读取文件的MD5值的方法
    C#对.CSV格式的文件--逗号分隔值文件 的读写操作及上传ftp服务器操作方法总结
    利用jQueryRotate旋转插件开发大转盘抽奖
    说说第三方支付接口开发及开发中遇到的坑爹问题
    浅谈程序员接私单那点事及接私单需要注意的问题
    C#微信公众号接口开发,灵活利用网页授权、带参数二维码、模板消息,提升用户体验之完成用户绑定个人微信及验证码获取
    C#.NET微信公众账号接口开发系列文章整理--微信接口开发目录,方便需要的博友查询
    C#/ASP.NET MVC微信公众号接口开发之从零开发(四) 微信自定义菜单(附源码)
    C#/ASP.NET MVC微信公众号接口开发之从零开发(三)回复消息 (附源码)
  • 原文地址:https://www.cnblogs.com/yaozhenfa/p/3550435.html
Copyright © 2011-2022 走看看