0

На экране использую 2 вкладки. В качестве вкладок у меня установлен кастомный layout (так как нужны разные цвета текста и имитация кнопки). Но почему-то добавился скрол к Tablayout всего для двух вкладок. Поэтому не виден правый край 2 вкладки. Пробовал применить атрибут tabMode="fixed" - не помогло

Разметка

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorFirst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_background_app_bar">

<android.support.design.widget.AppBarLayout
    android:id="@+id/mainAppbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/main_bg"
    android:fitsSystemWindows="true"
    android:paddingBottom="16dp"
    app:elevation="0dp"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/mainCollapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/transparent"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="enterAlways">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabsIndicator"
            style="@style/TabLayout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginHorizontal="16dp"
            android:layout_marginTop="16dp"
            android:padding="2dp"
            app:tabIndicator="@drawable/selector_background_tabs"
            app:tabIndicatorHeight="42dp"
            app:tabMode="fixed"
            app:tabRippleColor="@null"/>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<ru.diitcenter.peretz2.presentation.utils.NonSwipeableViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_constraintTop_toBottomOf="@+id/mainAppbar"
    app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

В коде

private fun initCustomTabs() {
    //Установка цветов при первоначальном запуске
    tabsIndicator.background = ContextCompat.getDrawable(
            tabsIndicator.context,
            R.drawable.round_dark_tab_layout
    )
    mainAppbar.setBackgroundColor(ContextCompat.getColor(
            mainAppbar.context,
            R.color.dark_background_app_bar))

    tabsIndicator.setSelectedTabIndicatorColor(ContextCompat.getColor(
            tabsIndicator.context,
            R.color.dark_background_app_bar
    ))

    //Отображение текста и иконки на первой вкладке
    val tabLinearLayout = LayoutInflater.from(context)
            .inflate(R.layout.custom_tabs_for_tab_layout, null) as LinearLayout
    val tabTitleLeft = tabLinearLayout.findViewById<View>(R.id.textViewTab) as TextView
    tabTitleLeft.text = getString(R.string.title_peretz)
    tabTitleLeft.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.logo, 0)
    tabTitleLeft.compoundDrawablePadding = 6
    tabTitleLeft.setTextColor(ContextCompat.getColor(
            tabTitleLeft.context,
            R.color.white
    ))

    //Отображение текста и иконки на второй вкладке
    val tabLinearLayout1 = LayoutInflater.from(context)
            .inflate(R.layout.custom_tabs_for_tab_layout, null) as LinearLayout
    val tabTitleRight = tabLinearLayout1.findViewById<View>(R.id.textViewTab) as TextView
    tabTitleRight.text = getString(R.string.title_green_apple)
    tabTitleRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_green_apple, 0)
    tabTitleRight.compoundDrawablePadding = 1
    tabTitleRight.setTextColor(ContextCompat.getColor(
            tabTitleLeft.context,
            R.color.titleRightGray
    ))

    tabsIndicator.getTabAt(0)?.customView = tabTitleLeft
    tabsIndicator.getTabAt(1)?.customView = tabTitleRight

    tabsIndicator.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {}
        override fun onTabUnselected(tab: TabLayout.Tab?) {}

        override fun onTabSelected(tab: TabLayout.Tab?) {
            when (tab?.position) {
                0 -> onLeftTabClicked(tabTitleLeft, tabTitleRight)
                1 -> onRightTabClicked(tabTitleLeft, tabTitleRight)
            }
        }
    })
}

1 ответ 1

0

Проблема решилась с помощью добавления метода, который устанавливает margin

 private fun setMargins(){
    //Левый tab
    val tabLeft = (tabsToolbar.getChildAt(0) as ViewGroup).getChildAt(0)
    val valuesLeft = tabLeft.layoutParams as ViewGroup.MarginLayoutParams

    valuesLeft.rightMargin = this.resources.getDimensionPixelSize(R.dimen.margin_selector)
    valuesLeft.leftMargin = this.resources.getDimensionPixelSize(R.dimen.margin_selector)

    //Правый tab
    val tabRight = (tabsToolbar.getChildAt(0) as ViewGroup).getChildAt(1)
    val valuesRight = tabRight.layoutParams as ViewGroup.MarginLayoutParams

    valuesRight.rightMargin = this.resources.getDimensionPixelSize(R.dimen.margin_selector)
    valuesRight.leftMargin = this.resources.getDimensionPixelSize(R.dimen.margin_selector)
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.