android - 自定义 FragmentTabHost 在底部设置 TabWidget

标签 android android-layout android-fragments android-xml

目前我正在尝试为我的项目实现 FragmentTabHost。我对这个 fragment 还很陌生,但我发现它在重用布局等方面非常棒,这就是为什么我想进一步插入自己。现在,我阅读了有关如何使用 fragment 创建选项卡的教程,并阅读了本教程:

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>

最佳答案

我已引用此链接 github example

这将是您的布局:

<?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);

结束编辑

如果要添加drawable(选择器):

<?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>

关于android - 自定义 FragmentTabHost 在底部设置 TabWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18527071/

相关文章:

android - 如何替换旧 API<21 中的 NestedScrollView?

java - 如何将 boolean 值从 DialogFragment 传递到 Android 中的另一个 Fragment?

javascript - DOMException : play() can only be initiated by a user gesture

具有两种颜色的Android布局背景

android - 无法在 Android 中创建外部文件目录。 WRITE_EXTERNAL_STORAGE 存在

android - 一旦高度为 0,LinearyLayout 将不会调整大小

Android点击项目菜单不起作用

java - Android - fragment 中两个按钮中的第二个按钮无响应

java - 导入 Wavefront .obj

android - 按名称加载可绘制对象