如何在Fragment中设置FragmentTabHost的样式

5
这是我的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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

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

        />

</LinearLayout>

这是托管的 Fragment。
public class TabPractice extends Fragment{
 private FragmentTabHost mTabHost;

//Mandatory Constructor
    public TabPractice() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tabs_practice,container, false);


        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("Practice All").setIndicator("Practice All"),
                WelcomeScreen.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("Practive Fav").setIndicator("Practive Fav"),
                FavoriteList.class, null);



        return rootView;

    }

}

图片描述

这是我的选项卡样式,我不知道如何对它进行样式设计。在大多数解释样式的例子中,XML 中有一个 TabWidget,但当我尝试将其添加到我的布局中时,它无法工作。

1) 有人能帮我理解如何在 Fragment 中托管 FragmenttabHost(来自某个教程的代码)与在 FragmentActivity 中拥有它的区别吗?

2) 如何在我的代码中为选项卡设置样式?

谢谢。


你找到解决方案了吗?我也很感兴趣。 - Klaus
请查看此答案:https://dev59.com/ZG855IYBdhLWcg3w0H53#5263790 - bogdan
1个回答

3
请将您的xml文件编写如下:
<?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" >

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

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

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

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/dark_blue" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />

            <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@android:color/white" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

以下是您的TabPracticeFragment中选项卡的代码:

mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);

mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

View tabIndicatorToday = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorToday.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.today));

mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.today)).setIndicator(tabIndicatorToday), EntriesTodayFragment.class, null);

View tabIndicatorLive = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorLive.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.live));

在drawable中,tab_indicator的xml如下:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_tab_txt"
                style="?android:attr/tabWidgetStyle"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="@string/live"
                android:textColor="@drawable/tab_text_color"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:layout_gravity="bottom"
                android:background="@drawable/tab_underline_selector" />
        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:id="@+id/tabs_divider_view"
            android:layout_height="30dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/grey_background" />

    </LinearLayout>

现在,您可以根据自己的喜好自定义标签的背景、高亮颜色和标签下方的线条。希望这对您有所帮助。


这太棒了,不是吗!即使过了2年...感谢@Logic - BENN1TH

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