Как установить разделитель между вкладками в FragmentTabHost библиотеки поддержки дизайна?

Я новичок в Android, у меня есть следующая вкладка, и мне нужно установить разделитель между вкладками < img src="https://i.stack.imgur.com/Og136.png" alt="введите здесь описание изображения">

Я хотел бы получить что-то вроде этого

введите здесь описание изображения

Я уже читал это, но решение приходит к 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>

Я также смотрел это и просмотрел пару мест, но, пожалуйста, спасибо им, если вы могли бы мне помочь


person Juan Pablo Moreno Martín    schedule 29.11.2019    source источник
comment
Установите свой drawable как атрибут android:divider в файле <TabWidget>.   -  person Mike M.    schedule 29.11.2019
comment
Я не понимаю   -  person Juan Pablo Moreno Martín    schedule 29.11.2019
comment
Добавьте android:divider="@drawable/tab_divider" к тегу <TabWidget> в макете.   -  person Mike M.    schedule 29.11.2019
comment
Я не решил проблему, по-прежнему ничего не показывает.   -  person Juan Pablo Moreno Martín    schedule 29.11.2019
comment
Что ж, это довольно глупо, но похоже, что вам нужно добавить еще и android:showDividers="middle", хотя это не упоминается в документах. По крайней мере, это то, что я должен был сделать, чтобы заставить его работать.   -  person Mike M.    schedule 29.11.2019
comment
Отлично, большое спасибо, вы не представляете, сколько мест я искал это решение, пока, наконец, вы мне не поможете   -  person Juan Pablo Moreno Martín    schedule 29.11.2019
comment
Да, это плохая документация. TabWidget является подклассом LinearLayout, и он просто передает все разделители в LinearLayout. В документах TabWidget конкретно упоминается только атрибут divider, поэтому можно подумать, что TabWidget сам обрабатывает соответствующий параметр showDividers, но, по-видимому, это не так. Ничего, нет проблем. Рад, что у тебя получилось. Ваше здоровье!   -  person Mike M.    schedule 29.11.2019


Ответы (1)


Благодаря комментариям @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" Не обязательно добиваться того, что вы хотите, с ним или без него появляются разделительные линии, но с этим можно их еще немного затемнить, надеюсь, это поможет кому-то еще.

person Juan Pablo Moreno Martín    schedule 29.11.2019