簡體   English   中英

在標簽選擇中,更改其他標簽圖標,並在android中以編程方式更改標簽文本的顏色

[英]On tab selection change other tabs icons and change the color of the tab text programmatically in android

我已經為每個選項卡開發了具有自定義布局的tabbedview。現在,當我選擇一個選項卡時,其他選項卡的圖標應該更改,並且我想更改其他選項卡文本的顏色。 請幫忙?

這是我的代碼:

          final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_one));
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_two));
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_three));
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_four));


    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final TabViewAdapter adapter = new TabViewAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());

            }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {


        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}
    // - set a tab listener to your tabLayout
    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {  
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                super.onTabUnselected(tab);
            }
            // - this method will get execute if you select some tab
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
                // - here change the color or image of your tab which is selected and of unselected also
            }
      } 

嘗試使用此tabSelectedTextColor =“ yourColor”,但是您在Value-> style上擁有樣式

   <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabMode="scrollable"
        app:tabGravity="fill"
        app:tabSelectedTextColor="@color/black"
        app:tabTextColor="@color/red" />

您可以在androidhive.com http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/中訪問有關Material Tab布局的精彩文章

只需閱讀本文,並了解如何更改“標簽”圖標和顏色。 要在View Pager項上進行更改,可以在View pager中設置PageChangeListener並根據新頁面更新選項卡。 如有任何疑問,請隨時發表評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM