如何在设计支持库的FragmentTabHost中的选项卡之间设置分隔符?

问题描述 投票:0回答:1

我是Android的新手,我具有以下标签,并且需要在标签之间建立分隔符enter image description here

我想得到这样的东西

enter image description here

我已经读过this,但是解决方案是TabLayout,并且我正在使用FragmentTabHost,但它不起作用

这是我尝试实现的解决方案

在我的活动中

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_flight);

        tabHost= (FragmentTabHost) findViewById(R.id.search_flight_tab_host);
        tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);

        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ROUND_TRIP).setIndicator(getString(R.string.round_trip)),
                SearchFlightRoundTripFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ONE_WAY).setIndicator(getString(R.string.one_way)),
                SearchFlightOneWayFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_MULTIPLE).setIndicator(getString(R.string.multiple)),
                SearchFlightMultipleFragment.class, null);

        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            View tabView = tabHost.getChildAt(i);
            if (tabView instanceof LinearLayout) {
                ((LinearLayout) tabView).setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
                Drawable drawable = getDrawable(R.drawable.tab_divider);
                ((LinearLayout) tabView).setDividerDrawable(drawable);
            }
        }
    }

我的可绘制tab_divider

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size android:width="1dp" />
            <solid android:color="@color/colorBlack" />
        </shape>
    </item>

    <item
        android:bottom="16dp"
        android:top="12dp">

        <shape android:shape="rectangle">
            <solid android:color="@color/colorBlack" />
            <size android:width="1dp" />
        </shape>
    </item>
</layer-list>

我的activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchFlightActivity" >

    <FrameLayout
        android:gravity="fill_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <ProgressBar
            android:id="@+id/top_progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:indeterminateTint="@color/colorAccent"
            android:indeterminate="true"
            android:max="100"
            android:visibility="gone" />

    </FrameLayout>

    <ScrollView
        android:layout_marginTop="11dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:background="@color/colorPrimaryDark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <android.support.v4.app.FragmentTabHost
                android:id="@+id/search_flight_tab_host"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_marginTop="6dp"
                    android:layout_marginRight="6dp"
                    android:orientation="vertical">

                    <TabWidget
                        android:id="@android:id/tabs"
                        style="@style/lightGrayLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:background="@drawable/tab_border_round"
                        android:orientation="horizontal" />

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

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

我也在看this,看过几个地方,但如果可以帮助我,请感谢他们

android android-tabs divider fragment-tab-host
1个回答
0
投票

由于@MikeM的评论,我得以解决问题,下一个解决方案是:

在活动中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_flight);

        tabHost= (FragmentTabHost) findViewById(R.id.search_flight_tab_host);
        tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);

        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ROUND_TRIP).setIndicator(getString(R.string.round_trip)),
                SearchFlightRoundTripFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_ONE_WAY).setIndicator(getString(R.string.one_way)),
                SearchFlightOneWayFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(Constants.SEARCH_FLIGHT_MULTIPLE).setIndicator(getString(R.string.multiple)),
                SearchFlightMultipleFragment.class, null);
    }

在我的活动xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchFlightActivity" >

    <FrameLayout
        android:gravity="fill_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <ProgressBar
            android:id="@+id/top_progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:indeterminateTint="@color/colorAccent"
            android:indeterminate="true"
            android:max="100"
            android:visibility="gone" />

    </FrameLayout>

    <ScrollView
        android:id="@+id/mainLayout"
        android:layout_marginTop="11dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:background="@color/colorPrimaryDark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <android.support.v4.app.FragmentTabHost
                android:id="@+id/search_flight_tab_host"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_marginTop="6dp"
                    android:layout_marginRight="6dp"
                    android:orientation="vertical">

                    <TabWidget
                        android:id="@android:id/tabs"
                        style="@style/lightGrayLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:background="@drawable/tab_border_round"
                        android:orientation="horizontal"
                        android:divider="@color/colorBlack"
                        android:showDividers="middle"/>

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

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

应注意,属性android:divider="@color/colorBlack"不一定要实现所需的功能,有或没有分界线都会出现,但是有了这个,可以使它们进一步变黑,希望这对您有所帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.