使用TabLayout时如何更改标签的背景色?

编程入门 行业动态 更新时间:2024-10-27 12:34:10
本文介绍了使用TabLayout时如何更改标签的背景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我在主要活动中的代码

This is my code in the main activity

public class FilterActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_filter); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), FilterActivity.this); viewPager.setAdapter(pageAdapter); // Give the TabLayout the ViewPager final TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } }

这是我在XML中的代码

And this is my code in the XML

<LinearLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/app_bar" layout="@layout/app_bar"> </include> <android.support.design.widget.TabLayout android:id="@+id/sliding_tabs" android:layout_width="fill_parent" style="@style/MyCustomTabLayout" android:layout_height="48dp"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" android:background="@android:color/white" /> </LinearLayout>

我想更改一个选项卡的背景色

I want to change the background color of one tab when it's selected

推荐答案

最终对我有用的方法与@如果我是DJ建议的类似,但tabBackground应该位于layout文件中,并且不是 ,因此它看起来像:

What finally worked for me is similar to what @如果我是DJ suggested, but the tabBackground should be in the layout file and not inside the style, so it looks like:

res/layout/somefile.xml :

res/layout/somefile.xml:

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

和选择器 res/drawable/tab_color_selector.xml :

and the selector res/drawable/tab_color_selector.xml:

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

更多推荐

使用TabLayout时如何更改标签的背景色?

本文发布于:2023-11-05 04:25:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:背景色   如何更改   标签   TabLayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!