zoukankan      html  css  js  c++  java
  • FragmentTabHost的使用方法总结

    开发商城上中时为了实现底部导航栏使用了fragmentTabHost方法,总结如下:

     1、Activity必须继承fragmentActivity方法。

     2.必须调用setup()方法 

     3.添加TabSpec  

    具体代码如下: 

    主要布局:

    activity_main: 

    <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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <FrameLayout
    android:id="@+id/realtabcontent"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:background="@color/colorAccent"
    />


    <android.support.v4.app.FragmentTabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>
    </LinearLayout>

    主要代码逻辑: 

    public class MainActivity extends AppCompatActivity {
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    private List<Tab> mTabs = new ArrayList<>(5);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initTab();

    }

    private void initTab() {
    Tab tab_home = new Tab(HomeFragment.class,R.string.home,R.mipmap.icon_home);
    Tab tab_hot = new Tab(HotFragment.class,R.string.hot,R.mipmap.icon_hot);
    Tab tab_category = new Tab(CategoryFragment.class,R.string.catagory,R.mipmap.icon_discover);
    Tab tab_cart = new Tab(CartFragment.class,R.string.cart,R.mipmap.icon_cart);
    Tab tab_mine= new Tab(MineFragment.class,R.string.mine,R.mipmap.icon_user);

    mTabs.add(tab_home);
    mTabs.add(tab_hot);
    mTabs.add(tab_category);
    mTabs.add(tab_cart);
    mTabs.add(tab_mine);

    mInflater = LayoutInflater.from(this);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

    for(Tab tab : mTabs){
    TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));
    tabSpec.setIndicator( buildindictor(tab));
    mTabHost.addTab(tabSpec,tab.getFragment(),null);

    }

    }
    private View buildindictor(Tab tab){
    View view = mInflater.inflate(R.layout.tab_indication,null);
    ImageView img = (ImageView) findViewById(R.id.icon_tab);
    TextView txt = (TextView) findViewById(R.id.txt_indicator);

    img.setBackgroundResource(tab.getIcon());
    txt.setText(tab.getTitle());
    return view;

    }
    } 
    在新建CartFragment:CategoryFragment;.HomeFragment;.HotFragment;.MineFragmen类,都必须继承v4包下的Fragment
    public class HotFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_hot,container,false);
    }
    }
  • 相关阅读:
    修改Cosbench源码 支持s3的 http range request 测试场景
    CEPH s3 java sdk PUT对象并在同一个PUT请求中同时设置ACL为 Public
    庆祝团队合著的《自主实现SDN虚拟网络与企业私有云》终于得以出版 --- 本人负责分布式存储部分的编写
    Cosbench测试 RGW S3 path_style_access=true模式支持
    RGW 系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    CEPH 使用SSD日志盘+SATA数据盘, 随OSD数目递增对性能影响的递增测试
    使用CEPH RGW admin ops API 进行用户user AK/SK管理的秘诀
    prometheus consul docker redis_exporter 自动注册配置
    Consul 使用手册(感觉比较全了)
    RDS for MySQL权限问题(错误代码:1227,1725)
  • 原文地址:https://www.cnblogs.com/qq1107625225/p/7029402.html
Copyright © 2011-2022 走看看