【问题标题】:Customize FragmentTabHost set TabWidget on bottom自定义 FragmentTabHost 在底部设置 TabWidget
【发布时间】:2013-08-30 07:16:22
【问题描述】:

目前我正在尝试为我的项目实施 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>

【问题讨论】:

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


    【解决方案1】:

    我已经参考了这个链接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>
    

    【讨论】:

    • 目前正在查看。 :)
    • 嗨,格鲁,测试了它,是的,它有效!但我有一个问题。 tabDividers 显示,我不知道将其关闭。以及我无法更改选项卡的颜色。我该怎么做?
    • @KaHeL 在堆栈上检查此答案以获取 remove tabDividers 并且第二个疑问您必须根据您想要的颜色更新您的 选择器
    • 是的,我已经从您提供的链接中获得了解决方案的概念,并且已经应用​​了它。再次感谢! :)
    猜你喜欢
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多