简体   繁体   English

如何在Android中以编程方式更改TabLayout背景

[英]How to change TabLayout background programmatically in Android

What i am doing is I am trying to change Toolbar , StatusBar and Tablayout color other than the default theme color pragmatically , i am able to successfully change the color of all widgets but getting some type of boundary of app theme's primary color in Tablayout don't know why ? 我正在做的是我试图实用地更改默认theme颜色以外的ToolbarStatusBarTablayout颜色,我能够成功更改所有widgets的颜色,但是在Tablayout获取应用程序主题的主要颜色的某种边界类型不知道为什么吗? can anyone tell me how to remove it ? 谁能告诉我如何删除它? Is any default style for it is defined as edges of Tablayout are also not rectangular. 是否定义为Tablayout边缘Tablayout是矩形的任何默认样式。

Screen : 屏幕:

在此,Tablayout边界处可见的绿色是应用程序主题颜色

xml : xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="@+id/app_bar_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".mainFlow.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        android:background="@color/colorPrimaryDark"
        app:tabIndicatorHeight="2dp"
        app:tabMode="fixed"
        app:tabIndicatorColor="@color/white"/>

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


<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"/>

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

Try this: 尝试这个:

Your xml: 您的xml:

<android.support.design.widget.TabLayout
    ....
    app:tabBackground="@drawable/tab_color_selector"
    ...
    />

and the selector res/drawable/tab_color_selector.xml: 和选择器res / drawable / tab_color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
    <item android:drawable="@color/tab_background_unselected"/>
</selector>

Or programmatically: 或以编程方式:

tabLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.tab_color_selector));

I had the exact same problem. 我有同样的问题。 I wanted to change the entire tablayout not only the tab selected because I am using it with day/night mode. 我想更改整个选项卡布局,不仅是更改所选的选项卡,因为我将其用于日/夜模式。 I solved it by checking the boolean that I have set for night theme prior to setting the content view in onCreate. 我通过在onCreate中设置内容视图之前检查为夜晚主题设置的布尔值来解决此问题。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences nightModeSwitchState = getSharedPreferences("NightModeSwitchState", 0);
    mNightModeSwitchState = nightModeSwitchState.getBoolean("NightModeSwitchState", false);

    if (mNightModeSwitchState) {
        setContentView(R.layout.activity_book_night);
    } else {
        setContentView(R.layout.activity_book);
    }

I have two layouts made both include tabLayouts. 我有两个布局,都包括tabLayouts。 One of them references a style file for tabLayout set to night mode colors with the dark background and the layout references a style file set to regular colors for the tablayout. 其中之一引用了将tabLayout的样式文件设置为深色背景的夜间模式颜色,而布局引用了将TabLayout的样式文件设置为常规颜色的样式文件。

<style name="CategoryTab" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@android:color/white</item>
    <item name="tabSelectedTextColor">@android:color/white</item>
    <item name="tabTextAppearance">@style/CategoryTabTextAppearance</item>
    <item name="tabBackground">@drawable/tab_regular_theme</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

<style name="CategoryTabNight" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@android:color/white</item>
    <item name="tabSelectedTextColor">@android:color/white</item>
    <item name="tabTextAppearance">@style/CategoryTabTextAppearance</item>
    <item name="tabBackground">@drawable/tab_layout_dark_mode</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

The style files reference different drawables that control the background colors as you can see above.. and here are the drawables... 样式文件引用了控制背景颜色的不同可绘制对象,如您在上面看到的..这是可绘制对象...

Here is night mode drawable: 这是夜间模式可绘制的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/dark_grey" android:state_selected="true"/>
<item android:drawable="@color/dark_grey"/>
</selector>

Here is regular mode drawable: 这是常规模式可绘制:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/app_bar" android:state_selected="true"/>
<item android:drawable="@color/app_bar"/>
</selector>

Everything else is the layouts are identical as not to mess anything up just the style files for the tablayouts are changed. 布局的其他所有方面都是相同的,只是不会更改TabLayout的样式文件而使任何内容混乱。 I hope this makes sense. 我希望这是有道理的。 I tested it and it works for me. 我测试了它,对我有用。 I have tried so many other things and nothing worked. 我尝试了很多其他事情,但没有任何效果。 It is important to call Shared Preference to get the value before checking the boolean. 在检查布尔值之前,调用Shared Preference以获得值很重要。 I hope this helps someone. 我希望这可以帮助别人。

In my case, I've set tabs background color as transparent: 就我而言,我将标签的背景色设置为透明:

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:tabBackground="@android:color/transparent"/>

and then I just set TabLayout background color in code: 然后我只是在代码中设置TabLayout背景色:

tabs.setBackgroundColor()

You can set the theme for the TabLayout. 您可以设置TabLayout的主题。

<style name="MyTabLayout" parent="android:Widget">
      <item name="tabBackground">@drawable/tab_background</item> 
</style>

and set this theme to your TabLayout. 并将此主题设置为您的TabLayout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM