【问题标题】:How to set the divider between tabs in FragmentTabHost of the design support library?如何在设计支持库的 FragmentTabHost 中设置选项卡之间的分隔符?
【发布时间】:2019-11-29 07:01:26
【问题描述】:

我是 Android 新手,我有以下标签,我需要在标签之间建立分隔线

我想要这样的东西

我已经在阅读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>

我的活动.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,看了几个地方,但如果你能帮助我,请感谢他们

【问题讨论】:

  • 将你的drawable设置为&lt;TabWidget&gt;android:divider属性。
  • 我不明白
  • android:divider="@drawable/tab_divider" 添加到布局中的&lt;TabWidget&gt; 标记中。
  • 我没有解决问题,仍然没有显示。
  • 嗯,这有点傻,但看起来你也必须添加android:showDividers="middle",即使文档中没有提到它。至少,我必须这样做才能使其正常工作。

标签: android android-tabs divider fragment-tab-host


【解决方案1】:

感谢@MikeM 的cmets 我能够解决这个问题,解决方案是下一个:

在活动中

    @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"这个属性不是一定要达到你想要的,有没有它,分界线都会出现,但是有了这个,还是可以再暗一点,希望这样帮助别人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多