查看选项卡的背景颜色

时间:2016-03-02 07:53:08

标签: android tabs android-viewpager

请帮助我,我是Android的新手,我正在使用查看寻呼机实现标签,我想更改所选标签的背景颜色,但我完成了。

这是我的XML文件代码。

<?xml version="1.0" encoding="utf-8"?>
<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">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/colortab"
            app:tabMode="scrollable"/>
    <!--app:tabSelectedTextColor="@color/colortab"-->

     <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        />
   </LinearLayout>

这是我的片段代码,我在其中更改当前标签的颜色。

public class mainfragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;

    FragmentTabHost tabhost;
    Fragment fragment;
    int position;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View  v = inflater.inflate(R.layout.main_fragment, container, false);

        viewPager = (ViewPager) v.findViewById(R.id.viewpager);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout = (TabLayout) v.findViewById(R.id.tabs);
        viewPager.setOnPageChangeListener(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) {

            }
        });





        setupViewPager(viewPager);

        // change the tab in run time  working here


        tabLayout.post(new Runnable() {
            @Override
            public void run() {

                tabLayout.setupWithViewPager(viewPager);


                ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());



                adapter.addFragment(new One(), "One");
                adapter.addFragment(new Two(), "Two");

                viewPager.setAdapter(adapter);

            }
        });
        // end here


        tabLayout.setupWithViewPager(viewPager);
        return v;
    }

    // This method call ViewPagerAdapter which have added two fragments
    private void setupViewPager(ViewPager viewPager) {


        ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

        Toast.makeText(getActivity(),"Main Fragment in setupViewPager",Toast.LENGTH_LONG).show();

        adapter.addFragment(new One(), "One");
                adapter.addFragment(new Two(), "Two");

        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);
        }
    }
}

其实我想改变当前的标签背景颜色,但我没有得到。所以请帮我解决这个问题。提前谢谢。

我有一个tabLayout,它使用的viewpager包含两个标签。我一直在尝试更改当前所选标签的背景颜色。

1 个答案:

答案 0 :(得分:0)

更改选定的选项卡背景颜色您需要将选择器定义为可绘制的,并且还具有所选/未选择状态的drawable。您也可以将customView设置为选项卡Read this

首先,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>

然后,在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>

然后,在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>

最后,在styles.xml中,指定要使用的选择器:

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">@drawable/tab_background</item>
</style>

Here is the link i'm pasting important code if link is unavailable