首页 > 想模仿开源中国源码实现FragmentTabHost实现底部栏,可是一直不能实现出来?(走过路过的大神看一下)

想模仿开源中国源码实现FragmentTabHost实现底部栏,可是一直不能实现出来?(走过路过的大神看一下)

想实现这样的效果:


渣渣的我却弄成这样:(什么鬼啊)

MainACtivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
        View.OnTouchListener, TabHost.OnTabChangeListener {

    @InjectView(android.R.id.tabhost)
    MyFragmentTabHost mTabHost;
    @InjectView(R.id.iv_quick_option)
    ImageView ivQuickOption;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("MainActivity", "OnCreate");
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {

        mTabHost.setup(MainActivity.this, getSupportFragmentManager(), R.id.realtabcontent);
        if (android.os.Build.VERSION.SDK_INT > 10) {
            mTabHost.getTabWidget().setShowDividers(0);
        }
        initTabs();

        // 中间按键图片触发
        ivQuickOption.setOnClickListener(this);

        mTabHost.setCurrentTab(0);
        mTabHost.setOnTabChangedListener(this);
    }

    private void initTabs() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        Log.d("MainActivity", "Tab的个数为" + size);
        for (int i = 0; i < size; i++) {
            MainTab mainTab = tabs[i];
            TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
            View indicator = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(mainTab.getResIcon());
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            if (i == 1) {
                indicator.setVisibility(View.INVISIBLE);
                mTabHost.setNoTabChangedTag(getString(mainTab.getResName()));
            }
            title.setText(getString(mainTab.getResName()));
            tab.setIndicator(indicator);
            tab.setContent(new TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            mTabHost.addTab(tab, mainTab.getCls(), null);

            mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_quick_option) {
            Toast.makeText(MainActivity.this, "嘿嘿", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    @Override
    public void onTabChanged(String tabId) {

    }
}

activity_main.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.jack.musicandchat.activity.MainActivity">
    <!--管理Fragment-->
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/windows_bg">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dip">

            <com.jack.musicandchat.widget.MyFragmentTabHost
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dip" />

            <View
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/div_white" />
        </RelativeLayout>

        <!-- 快速操作按钮-->
        <ImageView
            android:id="@+id/iv_quick_option"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:contentDescription="@null"
            android:src="@drawable/tab_icon_quick" />
    </FrameLayout>
</LinearLayout>

MainTab.java

public enum MainTab {
    MUSIC(0, R.string.main_tab_name_music, R.drawable.tab_icon_music, MusicFragment.class),

    QUICK(1, R.string.main_tab_name_quick, R.drawable.tab_icon_quick, null),

    CHAT(2, R.string.main_tab_name_chat, R.drawable.tab_icon_chat, ChatFragment.class);

    private int idx;
    private int resName;
    private int resIcon;
    private Class<?> cls;

    private MainTab(int idx, int resName, int resIcon, Class<?> cls) {
        this.idx = idx;
        this.resName = resName;
        this.resIcon = resIcon;
        this.cls = cls;
    }

    public int getIdx() {
        return idx;
    }

    public void setIdx(int idx) {
        this.idx = idx;
    }

    public int getResName() {
        return resName;
    }

    public void setResName(int resName) {
        this.resName = resName;
    }

    public int getResIcon() {
        return resIcon;
    }

    public void setResIcon(int resIcon) {
        this.resIcon = resIcon;
    }

    public Class<?> getCls() {
        return cls;
    }

    public void setCls(Class<?> cls) {
        this.cls = cls;
    }
}

都什么年代了,还在用tabhost,试试magicindicator吧


我推荐用 FragmentTabHost+FragmentActivity+Fragment实现较好,楼主你的做法有些过时了,而且开源中国也是这么做的啊。


不是开源了么,你去看下不就知道了么


一般实现底部导航栏是在一个activity对应的布局文件里用一个线性布局来充当底部导航栏,线性布局的样式就是你的底部导航栏样式,线性布局之上的部分全部留出以便添加不同的fragment,也就是你导航栏里面的那几个页面。


推荐tablayout+viewpager+fragment...效果更好

【热门文章】
【热门文章】