我正在做的是我试图改变Toolbar
,StatusBar
和Tablayout
颜色除了默认的theme
颜色实用,我能够成功地改变所有widgets
的颜色,但在Tablayout
唐'获得应用主题的主要颜色的某种类型的边界知道为什么吗?任何人都可以告诉我如何删除它?是否有任何默认样式定义为Tablayout
的边也不是矩形。
屏幕:
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>
试试这个:
你的xml:
<android.support.design.widget.TabLayout
....
app:tabBackground="@drawable/tab_color_selector"
...
/>
和选择器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>
或以编程方式:
tabLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.tab_color_selector));
我有同样的问题。我想改变整个tablayout不仅选择了标签,因为我在日/夜模式下使用它。我通过在onCreate中设置内容视图之前检查我为night主题设置的布尔值来解决它。
@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);
}
我有两个布局,包括tabLayouts。其中一个引用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>
样式文件引用了控制背景颜色的不同drawable,如上所示..这里是drawables ...
这是夜间模式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>
这是常规模式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>
其他一切都是布局是相同的,以免混乱任何东西只是改变了tablayouts的样式文件。我希望这是有道理的。我测试了它,它对我有用。我尝试了很多其他的东西,没有任何效果。在检查布尔值之前调用共享首选项以获取值非常重要。我希望这可以帮助别人。
您可以为TabLayout设置主题。
<style name="MyTabLayout" parent="android:Widget">
<item name="tabBackground">@drawable/tab_background</item>
</style>
并将此主题设置为TabLayout。