底部菜单实现(三)fragmentTabHost和fragment

一.FragmentTabHost的好处

在于可以更加灵活的设置按钮的样式.和轻易的与frgment切换进行绑定.节省了代码量

二.布局文件

主布局
<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.example.tabhost.MainActivity">
<FrameLayout
    android:id="@+id/tabContent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"></FrameLayout>
<android.support.v4.app.FragmentTabHost
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></android.support.v4.app.FragmentTabHost></LinearLayout>    
tab标签的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical">

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/></LinearLayout>   

三.代码

public class MainActivity extends AppCompatActivity {


@Bind(R.id.framlayout)
FrameLayout mFramlayout;
@Bind(R.id.btn1)
Button mBtn1;
@Bind(R.id.btn2)
Button mBtn2;
@Bind(R.id.btn3)
Button mBtn3;
@Bind(R.id.radio_group)
RadioGroup mRadioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    iniView();
    iniEvent();

}

private void iniEvent() {

    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            BaseFragment fragment = null;
            switch (checkedId) {
                case R.id.btn1:

                    fragment = FragmentFractory.getInstance().getBaseFragment(0);
                    break;
                case R.id.btn2:

                    fragment = FragmentFractory.getInstance().getBaseFragment(1);
                    break;
                case R.id.btn3:
                    fragment = FragmentFractory.getInstance().getBaseFragment(2);
                    break;

            }
            transaction.replace(R.id.framlayout, fragment).commit();
        }
    });
}

private void iniView() {
    getSupportFragmentManager().
            beginTransaction().
            replace(R.id.framlayout, FragmentFractory.getInstance().getBaseFragment(0))
            .commit();
}

}

代码:

public class MainActivity extends AppCompatActivity {
    private Class []clazz={Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class};
    private FrameLayout mContainer;
    private FragmentTabHost mTabHost;
    String []tag={"one","two","three","four"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContainer = (FrameLayout) findViewById(R.id.container);
        mTabHost = (FragmentTabHost) findViewById(R.id.tab);
        iniTab();
    }

    private void iniTab() {
        //初始化tabHOST标签
        mTabHost.setup(this,getSupportFragmentManager(),R.id.container);
        //给每一个标签关联frgemnt
        for (int i = 0; i <tag.length ; i++) {
            //给每一个标签添加标记
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tag[i]);
            //拿到每一个标签的布局文件
         View view =getView(i);
            //关联标签的布局
            tabSpec.setIndicator(view);
            //把标签添加到FragmnetTabhost
            mTabHost.addTab(tabSpec,clazz[i],null);


        }
    }

    private View getView(int i) {
        TextView text=new TextView(this);
        switch (i) {
            case 0:
                text.setText("首页");
                 break;
            case 1:
                text.setText("发现");
                 break;
            case 2:
                text.setText("购物");
                 break;
            case 3:
                text.setText("我的");
                 break;

        }
        return text;
    }
}

布局文件

<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.example.administrator.recovertabfragmenthost.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v4.app.FragmentTabHost>
</LinearLayout>

源码下载
http://download.csdn.net/detail/dygcomed/9605219

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值