0

В моем приложении реализованы вкладки с помощью TabHost и TabWidget. Как можно изменить цвет заголовков и цвет нижних линий(TabStrip)? И вообще,есть более глобальный вопрос: в некоторых примерах вкладки реализованы через TabLayout. Какая разница между моей реализацией и TabLayout? Что лучше использовать?

tab_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/rounded_main_background">

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

фрагмент, где создаются вкладки:

public class TradescreenViewpager1FirstFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.vpager1_1st_fragment, container, false);

        TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost);
        tabHost.setup();

        TabHost.TabSpec tabSpec = tabHost.newTabSpec("tag1");
        tabSpec.setContent(R.id.tab1);
        tabSpec.setIndicator("BUY BTC");
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec("tag2");
        tabSpec.setContent(R.id.tab2);
        tabSpec.setIndicator("SELL BTC");
        tabHost.addTab(tabSpec);

        tabHost.setCurrentTab(0);

        return view;
    }
}

Я пробовал добавлять собственный стиль в файл styles.xml. Меняется цвет полностью всей вкладки, а мне не нужно менять цвет фона.

UPD: Сейчас переделал под TabLayout, контент добавляю в с помощью xml. Но приложение падает.

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill">

        <android.support.design.widget.TabItem
            android:id="@+id/tab1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/colorGrey"
            app:tabSelectedTextColor="@color/colorGreen"
            app:tabTextColor="@color/color_main"
            android:text="BUY BTC">
... 

1 ответ 1

0

Используйте TabLayout. И вот ответ для него:

Если нету импорта у родителя - делаем

 xmlns:app="http://schemas.android.com/apk/res-auto"

Дальше параметры думаю понятны:

app:tabIndicatorColor="@color/color"
app:tabSelectedTextColor="@color/about_tab"
app:tabTextColor="@color/about_tab"
4
  • Пробую добавить TabLayout вместо TabHost, и приложение падает. Контент во вкладки добавляю с помощью TabItem. что не так?
    – Snuf
    26 сен 2018 в 13:53
  • Необходим адаптер. Можете тут посмотреть как реализовать fandroid.info/…
    – RomanK.
    26 сен 2018 в 14:45
  • Цвета поменял. А как можно еще изменить размер шрифта?
    – Snuf
    27 сен 2018 в 7:04
  • @Snuf, сначала нужно добавить стиль <style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"> <item name="android:textSize">14sp</item> </style> А потом указать этот стиль в виджете 'TabLayout': app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
    – RomanK.
    27 сен 2018 в 7:30

Ваш ответ

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

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