java - 自定义 Tabhost 和选项卡(使用 Fragments)

标签 java android android-fragments android-tabhost

我正在构建一个包含 tabhost 的应用程序,并且效果很好。我的问题是我不知道如何自定义 tabhost(背景颜色、选中和未选中的项目,...)。我的代码是:

主 Activity .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_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();
        }
    }

}

希望它能消除你的疑虑!

关于java - 自定义 Tabhost 和选项卡(使用 Fragments),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971925/

相关文章:

java - 通过查询字符串的 REST api

android - 使用光标但没有键盘进行文本编辑

Android Fragment 无法解析添加方法

android - 按后退按钮返回 Fragments

java - 哪种设计模式最适合这种场景

java - 使用反射创建任何类型的对象

android - 如何获取 View 的绝对坐标

安卓 :How to get Battery status without Broadcast Receiver for API level 17 and above?

android - Fragment.onCreateView 有空容器

java - 如何准确定义域服务