Android 自定义FragmentTabHost设置底部的TabWidget

Android 自定义FragmentTabHost设置底部的TabWidget,android,android-layout,android-fragments,android-xml,Android,Android Layout,Android Fragments,Android Xml,目前我正在尝试为我的项目实现FragmentTabHost。我对这个片段还是新手,但我发现它在重用布局等方面非常好,这就是为什么我想把自己进一步推向它。现在,我阅读了有关如何使用片段创建选项卡的教程,并学习了本教程: 现在这一切都很好,除了tabWidget位于布局的顶部,我希望它位于底部。我发现我需要在初始化所有选项卡后设置tabWidget,因此我尝试添加以下代码: mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

目前我正在尝试为我的项目实现FragmentTabHost。我对这个片段还是新手,但我发现它在重用布局等方面非常好,这就是为什么我想把自己进一步推向它。现在,我阅读了有关如何使用片段创建选项卡的教程,并学习了本教程:

现在这一切都很好,除了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>

我已经参考了这个链接

这将是您的布局:

<?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);
    }
<?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>
编辑

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

如果要添加可绘制(选择器):

<?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);
    }
<?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在堆栈上检查这个答案,如果有第二个疑问,你必须根据你想要的颜色更新选择器。是的,我已经从你给出的链接中得到了解决方案的概念,并且已经应用了它。再次感谢!:)