底部菜单栏:FragmentTabHost

来源:互联网 发布:淘宝手机秒杀技巧 编辑:程序博客网 时间:2024/06/08 19:28

1,继承 FragmentActvitiy

public class MainActivity extends FragmentActivity 

2,Xml 文件的标准写法

<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.vily.cartshop.act.MainActivity">    <FrameLayout        android:id="@+id/realTabContent"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:layout_marginTop="5dp"        android:layout_marginBottom="5dp"        android:background="@color/bg_color"        >    </FrameLayout>    <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/white"        >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0"            >        </FrameLayout>    </android.support.v4.app.FragmentTabHost></LinearLayout>


3,oncreate 主页面代码:


protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iniView();        initData();    }    private void iniView() {        mRealTabContent = (FrameLayout) findViewById(R.id.realTabContent);        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);        mInflater = LayoutInflater.from(this);    }    private void initData() {        //   给tabhost 添加 tab        mTabHost.setup(this,getSupportFragmentManager(),R.id.realTabContent);        initTab();    }    private void initTab() {        Tab tab_home=new Tab(R.string.home,R.drawable.tab_home_icon_selector,HomeFragment.class);        Tab tab_hot=new Tab(R.string.hot,R.drawable.tab_hot_icon_selector,HotFragment.class);        Tab tab_discover=new Tab(R.string.discover,R.drawable.tab_discover_icon_selector,DiscoverFragment.class);        Tab tab_cart=new Tab(R.string.cart,R.drawable.tab_cart_icon_selector,CartFragment.class);        Tab tab_user=new Tab(R.string.user,R.drawable.tab_user_icon_selector,UserFragment.class);        mTabList.add(tab_home);        mTabList.add(tab_hot);        mTabList.add(tab_discover);        mTabList.add(tab_cart);        mTabList.add(tab_user);        for (Tab tab:mTabList){            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.title));            tabSpec.setIndicator(buildIndicator(tab));            //   给 tab 添加view 和 Fragment            mTabHost.addTab(tabSpec, tab.fragment,null);        }        mTabHost.setCurrentTab(0);    }    private View buildIndicator(Tab tab){        View view= mInflater.inflate(R.layout.tab_indicator, null);        ImageView tab_icon = (ImageView) view.findViewById(R.id.tab_icon);        TextView tab_title = (TextView) view.findViewById(R.id.tab_title);        tab_title.setText(tab.title);        tab_icon.setBackgroundResource(tab.icon);        return view;    }


阅读全文
0 0
原创粉丝点击