0

我是 Android 新手,我有以下选项卡,我需要在选项卡之间建立分隔线 在此处输入图像描述

我想得到这样的东西

在此处输入图像描述

我已经在阅读这个,但解决方案来自 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>

我的活动.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>

我也在看这个,看了几个地方,但如果你能帮助我,请感谢他们

4

1 回答 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"它不是必须实现你想要的,无论有没有它,分界线都会出现,但是有了这个,可以将它们变暗一点,我希望这对其他人有帮助。

于 2019-11-29T22:25:50.780 回答