在主题中定义TabLayout样式

20

我的应用中有两种不同样式的TabLayout

<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@android:color/white</item>
    <item name="tabTextColor">@android:color/white</item>
</style>

<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@android:color/black</item>
    <item name="tabTextColor">@android:color/black</item>
</style>

我该如何为主题定义默认的TabLayout样式?我找不到任何信息来确定应该使用哪个项目名称来构建我的主题。我想像添加列表视图那样添加TabLayout:

我该如何为主题定义默认的TabLayout样式?我找不到任何信息来确定应该使用哪个项目名称来构建我的主题。我想像添加列表视图那样添加TabLayout:

<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/black</item>
        <item name="colorPrimaryDark">@color/black</item>
        <item name="colorAccent">@color/main_red</item>
        <item name="android:windowBackground">@color/main_bg_light</item>
        <item name="android:listViewStyle">@style/MyListView</item>
    </style>

你找到解决方案了吗? - ffleandro
不是的。我必须手动实现视图更改。 - sliwkacz
@sliwkacz,我找到了一个真正有效的答案!你能把那个标记为正确的吗? - Flummox - don't be evil SE
4个回答

16

对于Support Library中的TabLayout,您可以在主题中设置tabStyle属性:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- ... -->
    <item name="tabStyle">@style/AppTheme.TabLayout</item>
</style>

<style name="AppTheme.TabLayout" parent="Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@android:color/white</item>
    <item name="tabTextColor">@android:color/white</item>
</style>

如果我使用 tabStyle,它就无法编译。 - Mark Buikema
1
@MarkBuikema 它适用于com.google.android.material.tabs.TabLayout,但不一定适用于标准的TabLayout - Slav

7
如果您想根据应用程序的主题为您的TabLayout使用2种不同的样式,则应在attrs.xml中定义您的样式。
以下是示例代码:

首先,您需要创建一个attrs.xml文件( 像这样

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textColor" format="reference|color" />
    <attr name="backColor" format="reference|color" />
    <attr name="myTabStyle" format="reference" />
</resources>

styles.xml 中根据您的要求定义各种主题

注意:我在演示中使用了3种不同的主题

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="tabStyle">@style/TabLayoutPower</item>
        <item name="textColor">@android:color/white</item>
        <item name="backColor">@color/colorPrimary</item>
    </style>

    <style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@android:color/holo_green_dark</item>
        <item name="colorPrimaryDark">@android:color/white</item>
        <item name="colorAccent">@android:color/holo_blue_dark</item>
        <item name="textColor">@android:color/white</item>
        <item name="tabStyle">@style/TabLayoutFree</item>
        <item name="backColor">#FF00</item>

    </style>

    <style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#ff00</item>
        <item name="colorPrimaryDark">#ff0</item>
        <item name="colorAccent">#0a91d4</item>
        <item name="tabStyle">@style/TabLayoutNew</item>
        <item name="textColor">#FF00</item>
        <item name="backColor">@android:color/white</item>
    </style>

    <style name="TabLayoutPower" parent="Widget.Design.TabLayout">
        <item name="tabSelectedTextColor">@android:color/white</item>
        <item name="tabTextColor">@android:color/white</item>
    </style>

    <style name="TabLayoutFree" parent="Widget.Design.TabLayout">
        <item name="tabSelectedTextColor">@android:color/holo_blue_bright</item>
        <item name="tabTextColor">@android:color/holo_blue_bright</item>
    </style>

    <style name="TabLayoutNew" parent="Widget.Design.TabLayout">
        <item name="tabSelectedTextColor">@android:color/holo_green_dark</item>
        <item name="tabTextColor">@android:color/holo_green_dark</item>
    </style>

</resources>

现在在您的layout.xml文件中像这样使用TabLayout中的stylestyle="?myTabStyle",它将根据您应用程序的当前主题选择style
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnThemeOne"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Theme One" />

        <Button
            android:id="@+id/btnThemeTwo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Theme Two" />


        <Button
            android:id="@+id/btnThemeThree"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Theme Three" />


    </LinearLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        style="?myTabStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        android:background="@color/colorAccent"
        app:tabMode="fixed" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

TabsActivity的示例代码,用于更改主题。

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class TabsActivity extends AppCompatActivity {

    PrefManager prefManager;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Button btnThemeOne, btnThemeTwo, btnThemeThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        prefManager = new PrefManager(this);
        getTheme().applyStyle(prefManager.getTheme(), true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);


        btnThemeOne = findViewById(R.id.btnThemeOne);

        btnThemeOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(TabsActivity.this, "Applying theme one", Toast.LENGTH_SHORT).show();
                prefManager.setTheme(R.style.AppTheme);
                recreate();
            }
        });

        btnThemeTwo = findViewById(R.id.btnThemeTwo);

        btnThemeTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(TabsActivity.this, "Applying theme two", Toast.LENGTH_SHORT).show();
                prefManager.setTheme(R.style.AppTheme2);
                recreate();
            }
        });


        btnThemeThree = findViewById(R.id.btnThemeThree);

        btnThemeThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(TabsActivity.this, "Applying theme three", Toast.LENGTH_SHORT).show();
                prefManager.setTheme(R.style.AppTheme3);
                recreate();
            }
        });

        viewPager = findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }


    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(), "ONE");
        adapter.addFragment(new OneFragment(), "TWO");
        adapter.addFragment(new OneFragment(), "THREE");

        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}
类用于保存主题信息。
import android.content.Context;
import android.content.SharedPreferences;

public class PrefManager {

    // Shared preferences file name
    private final String PREF_NAME = "theme-pref";
    private final String THEME = "theme";
    SharedPreferences pref;
    SharedPreferences.Editor editor;


    public PrefManager(Context context) {
        pref = context.getSharedPreferences(PREF_NAME, 0);
        editor = pref.edit();
    }

    public int getTheme() {
        return pref.getInt(THEME, R.style.AppTheme);
    }

    public void setTheme(int themeId) {
        editor.putInt(THEME, themeId);
        editor.commit();
    }

}

你可以检查上述代码的输出视频。

https://www.youtube.com/watch?v=uup072IDGd0


-2
    <style name="Base.Widget.Design.TabLayout" parent="">
        <item name="tabSelectedTextColor">@android:color/white</item>
        <item name="tabTextColor">@android:color/white</item>
    </style>

<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/black</item>
        <item name="colorPrimaryDark">@color/black</item>
        <item name="colorAccent">@color/main_red</item>
        <item name="android:windowBackground">@color/main_bg_light</item>
        <item name="android:listViewStyle">@style/MyListView</item>
<item name="android:tabWidgetStyle">@style/Base.Widget.Design.TabLayout</item>
    </style>

对我来说可以这样做 - 只需将父项保留为空并应用于android:tabWidgetStyle即可 ;) - Raphael S

-2
你可以在安卓主题中找到这个名字 -
<item name="android:tabWidgetStyle">@style/Your.Style</item>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接