简体   繁体   English

在TabLayout(Android设计支持库)中更改选项卡的背景颜色不会占用整个选项卡空间

[英]Changing the background color of a Tab in TabLayout (Android design support library) doesn't occupy the entire tab space

I have a TabLayout (design support library) which is tied up to a ViewPager containing three tabs. 我有一个TabLayout(设计支持库),它与包含三个选项卡的ViewPager绑定在一起。 I have designed a custom layout and set that to each tab in the TabLayout. 我设计了一个自定义布局,并将其设置为TabLayout中的每个选项卡。 I have been trying to change the background color of the currently selected tab. 我一直在尝试更改当前所选选项卡的背景颜色。 The color only wraps up around the text in the tab but doesn't occupy the entire tab space. 颜色仅包含在选项卡中的文本周围,但不占用整个选项卡空间。

Below are the code snippets of my activity and the custom layout file. 以下是我的活动和自定义布局文件的代码段。

Activity code 活动代码

public class CustomTabLayoutActivity extends AppCompatActivity {

private TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_tab_layout);
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    setupViewPager(viewPager);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    setupTabLayout();
    viewPager.setCurrentItem(0);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                if (i == position) {
                    tabLayout.getTabAt(i).getCustomView().setBackgroundColor(Color.parseColor("#198C19"));
                } else {
                    tabLayout.getTabAt(i).getCustomView().setBackgroundColor(Color.parseColor("#f4f4f4"));
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}


private void setupViewPager(ViewPager viewPager) {
    CustomViewPagerAdapter pagerAdapter = new CustomViewPagerAdapter(getSupportFragmentManager());
    pagerAdapter.addFragments(new OneFragment(), "ONE");
    pagerAdapter.addFragments(new OneFragment(), "TWO");
    pagerAdapter.addFragments(new OneFragment(), "THREE");
    viewPager.setAdapter(pagerAdapter);
}

private void setupTabLayout() {

    TextView customTab1 = (TextView) LayoutInflater.from(CustomTabLayoutActivity.this)
            .inflate(R.layout.custom_tab_layout, null);
    TextView customTab2 = (TextView) LayoutInflater.from(CustomTabLayoutActivity.this)
            .inflate(R.layout.custom_tab_layout, null);
    TextView customTab3 = (TextView) LayoutInflater.from(CustomTabLayoutActivity.this)
            .inflate(R.layout.custom_tab_layout, null);
    customTab1.setText("ONE");
    tabLayout.getTabAt(0).setCustomView(customTab1);
    customTab2.setText("TWO");
    tabLayout.getTabAt(1).setCustomView(customTab2);
    customTab3.setText("THREE");
    tabLayout.getTabAt(2).setCustomView(customTab3);
}
}

Custom Layout file for each tab 每个选项卡的自定义布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/tab"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_gravity="center"
   android:background="#ffffff"
   android:text="Test"
   android:textColor="@android:color/black"
   android:textSize="20sp" />

Here is the screenshot of the tabs after running the above code. 以下是运行上述代码后选项卡的屏幕截图。

在此输入图像描述

As you guys can see, the color only occupies the text in the tab but not the entire tab space. 正如大家们所看到的,颜色只占据选项卡中的文本而不占整个选项卡空间。 How to achieve this? 怎么做到这一点? Any ideas/suggestions would help me a lot. 任何想法/建议都会对我有所帮助。 Thanks in advance. 提前致谢。

Define a selector as a drawable, and also have a drawable for the selected/unselected states. 将选择器定义为可绘制的,并且还具有所选/未选择状态的可绘制。

For this solution, I started with the code from this answer , and then added the functionality that changes the background color for the current Tab. 对于此解决方案,我从这个答案的代码开始,然后添加了更改当前Tab的背景颜色的功能。

First, the selector, tab_background.xml in the drawable folder: 首先,drawable文件夹中的selector, tab_background.xml

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

Then, tab_background_selected.xml in the drawable folder: 然后,drawable文件夹中的tab_background_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#d13fdd1a" />
</shape>

Then, tab_background_unselected.xml in the drawable folder: 然后,drawable文件夹中的tab_background_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#3F51B5" />
</shape>

Finally, in your styles.xml , specify the selector to use, and also specify the tab indicator style, since the app:tabIndicatorColor property in the TabLayout will now be ignored: 最后,在styles.xml ,指定要使用的选择器,并指定选项卡指示器样式,因为现在将忽略TabLayout中的app:tabIndicatorColor属性:

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
    <item name="tabBackground">@drawable/tab_background</item>
    <item name="tabIndicatorColor">#ff00ff</item>
    <item name="tabIndicatorHeight">2dp</item>
</style>

Result with the example colors above: 以上示例颜色的结果:

在此输入图像描述

在此输入图像描述

Additional Note: 附加说明:

Tested with the 23.3.0 versions of the support library components: 使用23.3.0版本的支持库组件进行测试:

dependencies {
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
    compile 'com.android.support:recyclerview-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
}

you should use: 你应该使用:

app:tabBackground="@drawable/tab_selector"
android:background="@color/colorNormal"

tab_selector.xml (in Drawable Folder): tab_selector.xml(在Drawable文件夹中):

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

Tabs with Ripple effect: In addition to Daniel Nugent's answer It would be beautiful to add a ripple effect to tabs. 带有涟漪效应的标签:除了Daniel Nugent的答案之外,为标签添加涟漪效果会很漂亮。 In order to achieve this, you must add these two drawables to drawable-v21 folder: 为了实现这一点,您必须将这两个drawable添加到drawable-v21文件夹:

tab_background_selected.xml : tab_background_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#63D25B"> <!-- ripple color -->
    <item android:drawable="#d13fdd1a" /> <!-- normal color -->
</ripple>

tab_background_unselected.xml : tab_background_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#606FC7"> <!-- ripple color -->
    <item android:drawable="#3F51B5" /> <!-- normal color -->
</ripple>

I know that its quite late to answer this question, but have a different & simple answer without creating any new background or selector. 我知道回答这个问题已经很晚了,但是在没有创建任何新背景或选择器的情况下有一个不同的简单答案。 Tab-Layout have default padding of 12dp at its start & end. Tab-Layout在其开始和结束时具有12dp的默认填充。 Just set 刚设置

app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"

to fill color in your tab. 填写选项卡中的颜色。

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

相关问题 TabLayout(Android支持设计)中的选项卡不会使内容膨胀 - Tab in TabLayout (Android Support Design) doesn't inflate content 在Android设计库中使用TabLayout的图标选项卡 - Tab with icon using TabLayout in Android Design Library 无法在 TabLayout 设计支持库中看到 Tab Indicator - Unable to see Tab Indicator in TabLayout design support library 更改TabLayout(android.support.design.widget)的选定选项卡指示器颜色 - change the selected tab indicator color of TabLayout(android.support.design.widget) 如何在使用android.support.design.widget.TabLayout的TabLayout时设置特定的Tab并获取Current Tab; - How to set particular Tab and get Current Tab while using TabLayout of android.support.design.widget.TabLayout; android.support.design.widget.TabLayout以编程方式选择标签 - android.support.design.widget.TabLayout select Tab Programmatically android.support.design.widget.tablayout选项卡指示器颜色 - android.support.design.widget.tablayout Tab Indicator colour android 设计支持TabLayout 更改标签文字字体 - Change the font of tab text in android design support TabLayout 开始时选择Android Design Support Library选项卡 - Android Design Support Library Tab selection on start Android TabLayout选择了标签背景 - Android TabLayout selected tab background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM