簡體   English   中英

自定義Tabhost和選項卡(帶有片段)

[英]Customize Tabhost and tabs (with Fragments)

我正在構建一個包含tabhost的應用程序,它運行良好。 我的問題是我不知道如何自定義tabhost(背景顏色,選定和未選定的項目,...)。 我的代碼是:

MainActivity.java

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home"),
                HomeFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("que hacer").setIndicator("¿Qué hacer?"),
                HacerFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("donde").setIndicator("¿A dónde ir?"),
                DestacadoFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("mapa").setIndicator("Lee tu mapa"),
                LectorFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("captura").setIndicator("Captura"),
                CapturaFragment.class, null);

我的布局是這樣的:

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

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="50dip"
                        android:layout_weight="0"/>

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0"/>

                </LinearLayout>
            </android.support.v4.app.FragmentTabHost>

非常感謝 ;)

為了您的關心,您需要像這樣實現選擇器..

您的Tab選擇器..希望U可以實現其余的選擇器

tab_select_learn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/learn" />
    <item android:drawable="@drawable/learng" />
</selector>

並像這樣使用..

您的Home.java類

public class Home extends FragmentActivity {

    private static final String TAB_1_TAG = "tab_1";
    private static final String TAB_2_TAG = "tab_2";
    private static final String TAB_3_TAG = "tab_3";
    private static final String TAB_4_TAG = "tab_4";
    private static final String TAB_5_TAG = "tab_5";
    private FragmentTabHost mTabHost;
    View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.home);
        initView();
        view = (View) findViewById(R.drawable.tab_indicator);
        // setTabs();
    }

    private void initView() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec(TAB_1_TAG).setIndicator("",
                        getResources().getDrawable(R.drawable.tab_select_talk)),
                TalkContainerFragment.class, null);
        // mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("Talk"),
        // TalkContainerFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec(TAB_2_TAG).setIndicator("",
                        getResources().getDrawable(R.drawable.tab_select_learn)),
                LearnContainerFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec(TAB_3_TAG).setIndicator("",
                        getResources().getDrawable(R.drawable.tab_select_go)),
                GoContainerFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec(TAB_4_TAG).setIndicator("",
                        getResources().getDrawable(R.drawable.tab_select_watch)),
                WatchContainerFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec(TAB_5_TAG).setIndicator("",
                        getResources().getDrawable(R.drawable.tab_select_more)),
                MoreContainerFragment.class, null);

        // Inflating color for the first time.
        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundColor(Color.parseColor("#181818"));
        }

        mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
                .setBackgroundColor(Color.parseColor("#424542"));
        // ============== End of color inflation ==================

        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {

                // Inflating color when tab is selected.
                for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
                    mTabHost.getTabWidget().getChildAt(i)
                            .setBackgroundColor(Color.parseColor("#181818"));

                }

                mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
                        .setBackgroundColor(Color.parseColor("#424542"));
                // ============== End of color inflation ==================

            }
        });

        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
            final TextView tv = (TextView) mTabHost.getTabWidget()
                    .getChildAt(i).findViewById(android.R.id.title);
            // mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.talk);
            if (tv == null)
                continue;
            else
                tv.setTextSize(8);

        }

    }

    @Override
    public void onBackPressed() {
        boolean isPopFragment = false;
        String currentTabTag = mTabHost.getCurrentTabTag();
        if (currentTabTag.equals(TAB_1_TAG)) {
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_1_TAG)).popFragment();
        } else if (currentTabTag.equals(TAB_2_TAG)) {
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_2_TAG)).popFragment();
        } else if (currentTabTag.equals(TAB_3_TAG)) {
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_3_TAG)).popFragment();
        } else if (currentTabTag.equals(TAB_4_TAG)) {
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_4_TAG)).popFragment();
        } else if (currentTabTag.equals(TAB_5_TAG)) {
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_5_TAG)).popFragment();
        }
        if (!isPopFragment) {
            finish();
        }
    }

}

希望它消除您的疑慮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM