fragmentTabhost+Fragment底部菜单实现方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lg491733638/article/details/50493453

最近在研究底部菜单的实现方法,之前用的是viewpager+ fragment和radiobutton+fragment,但是代码非常多,谷歌在最新的api上用了fragmentTabhost实现起来比较简单,好了废话不多说,先看运行效果。

1.今天的知识点:fragmentTabhost的用法和selector的用法

下面直接上代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--存放主要内容 -->
    <FrameLayout
        android:id="@+id/maincontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

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

        <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>

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="3dp"
    android:paddingTop="3dp">

    <ImageView
        android:id="@+id/image_tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name" />

    <TextView
        android:id="@+id/text_tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp" />


</LinearLayout>
 
MainActivity.java
 
package bailead.com.sports;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import bailead.com.sports.entity.Tab;
import bailead.com.sports.fragment.HomeFragment;
import bailead.com.sports.fragment.IntroFragment;
import bailead.com.sports.fragment.SettingFragment;

/**
 * Created by Administrator on 2016/1/9.
 */
public class MainActivity extends FragmentActivity {

    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;

    private List<Tab> list = new ArrayList<>();

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

    private void initTab() {
        Tab home = new Tab(R.string.string_tab_home, R.drawable.selcet_image_home, HomeFragment.class);
        Tab intro = new Tab(R.string.string_tab_intro, R.drawable.selcet_image_intro, IntroFragment.class);
        Tab setting = new Tab(R.string.string_tab_setting, R.drawable.selcet_image_setting, SettingFragment.class);
        list.add(home);
        list.add(intro);
        list.add(setting);
        mInflater = LayoutInflater.from(MainActivity.this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(MainActivity.this, getSupportFragmentManager(), R.id.maincontent);

        for (Tab tab : list) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(String.valueOf(tab.getTitle()));
            tabSpec.setIndicator(buildIndicator(tab));
            mTabHost.addTab(tabSpec, tab.getFragment(), null);
        }
        //去掉分割线
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setCurrentTab(0);
    }

    public View buildIndicator(Tab tab) {
        View view = mInflater.inflate(R.layout.tab_indicator, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.image_tab);
        TextView textView = (TextView) view.findViewById(R.id.text_tab);
        imageView.setBackgroundResource(tab.getImage());
        textView.setText(tab.getTitle());
        return view;
    }

}

猜你喜欢

转载自blog.csdn.net/lg491733638/article/details/50493453