定制FragmentTabHost设置TabWidget在底部

6

我目前正在尝试为我的项目实现FragmentTabHost。虽然我对这些片段还很陌生,但我发现它在重用布局等方面非常棒,这就是为什么我想进一步深入了解它的原因。现在我阅读了有关如何创建带有片段的选项卡的教程,并找到了这个教程:

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

现在这个程序可以正常运行,但是tabWidget在我想让它在底部的布局上方。我发现需要在所有标签都被初始化之后设置tabWidget,所以我尝试添加以下代码:
mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

    mTabWidget.setBackgroundColor(Color.WHITE);
    mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
    mTabWidget.setGravity(Gravity.BOTTOM);

现在这个已经消除了分隔线并改变了颜色,但显然不会将我的小部件放在布局的底部。现在我该怎么做?
我还尝试编辑Tabhost xml,只是将TabWidget放在FrameLayout之后,但没有任何反应。这是xml:
    <android.support.v4.app.FragmentTabHost
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

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

                <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:orientation="horizontal"
                        />

            </LinearLayout>

        </android.support.v4.app.FragmentTabHost>
1个回答

13

我参考了这个链接 github示例

这将是您的布局:

<?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/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        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" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

针对您的自定义标签:

mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"),
                        R.drawable.image1),

    public TabSpec setIndicator(Context ctx,TabSpec spec, int resid) {
        // TODO Auto-generated method stub
        View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text, null);
        v.setBackgroundResource(resid);
        TextView text = (TextView) v.findViewById(R.id.tab_title);
        text.setText(spec.getTag());
        return spec.setIndicator(v);
    }

编辑

 //To set drawable to your perticular TAB 
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login);

结束编辑

如果你想要添加可绘制对象(选择器):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/tab_compose_h" android:state_selected="true"/>
    <item android:drawable="@drawable/tab_compose_h" android:state_pressed="true"/>
    <item android:drawable="@drawable/tab_compose"/>

</selector>

目前正在检查。 :) - KaHeL
嗨Gru,我测试了一下,是可以的!但是我有一个问题。tabDividers显示出来了,我不知道该如何关闭它。而且我也无法更改选项卡的颜色。我该怎么做? - KaHeL
@KaHeL,请查看Stack上有关删除tabDividers的答案,对于你的第二个疑问,你需要根据你想要的颜色更新你的选择器 - Gru
是的,我已经从你给出的链接中了解到了解决方案的概念,并已经应用它。再次感谢! :) - KaHeL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接