其他分享
首页 > 其他分享> > android-如何在自定义选项卡布局中更改选项卡布局所选文本的颜色

android-如何在自定义选项卡布局中更改选项卡布局所选文本的颜色

作者:互联网

我有viewpager有4个选项卡,用tablayout实现它.因为我想将字体更改为自定义字体,所以我使用了自定义标签布局.现在的问题是,所选的标签文本颜色不会随着tabSelectedTextColor改变.

这是我的Java代码:

package com.example.noavaran.drugstore.Activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.noavaran.drugstore.Fragment.AddressFragment;
import com.example.noavaran.drugstore.Fragment.IssueFragment;
import com.example.noavaran.drugstore.Fragment.MenuFragment;
import com.example.noavaran.drugstore.Fragment.SearchFragment;
import com.example.noavaran.drugstore.Helper.CustomViewPager;
import com.example.noavaran.drugstore.R;

import java.util.ArrayList;
import java.util.List;

import layout.fragment_approve;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
import uk.co.chrisjenx.calligraphy.CalligraphyUtils;

public class MainPageActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    public Context context;
    public Typeface tf;
    public static boolean a=false;
    private CustomViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.menu,
            R.drawable.address_icon,
            R.drawable.search_icon,
            R.drawable.issue_icon
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
         tf = Typeface.createFromAsset(getAssets(), "IRANSans_Bold.ttf");

        tabCustomization();
        /*changeTabsFont();*/
        setupTabIcons();
    }

    private void tabCustomization() {
        viewPager = (CustomViewPager) findViewById(R.id.viewPager);
        viewPager.setPagingEnabled(false);

        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);

        tabLayout.setupWithViewPager(viewPager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());


        adapter.addFragment(new MenuFragment(), "menu");

        adapter.addFragment(new AddressFragment(), "Address");
        adapter.addFragment(new SearchFragment(), "Searcg");
        adapter.addFragment(new IssueFragment(), "Issue");

        viewPager.setAdapter(adapter);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);

            tab.setCustomView(adapter.getTabView(tabLayout,i));
        }


        ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        int tabsCount = vg.getChildCount();
        Log.i("TabChild",String.valueOf(tabsCount));


// Iterate over all tabs and set the custom view

    }
    public void setupViewPager(ViewPager viewPager) {

    }
    private void setupTabIcons() {

        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[3]);


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){


            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
            }

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

            }
        });
    }


    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);
        }
        public View getTabView(TabLayout tabLayout,int position) {
            // Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView

            View view = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.custom_tab, tabLayout, false);
            TextView textView= (TextView) view.findViewById(R.id.tabText);

            textView.setText(getPageTitle(position));
            textView.setTypeface(tf);

            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            imageView .setImageResource(tabIcons[position]);

            return view;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }

    }

    //Caligraphy Library
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
}

我的自定义布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5px"
        android:id="@+id/imageView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="New Text"
        android:textColor="@android:color/white"
        android:gravity="center"

        android:id="@+id/tabText" />
</LinearLayout>

这是我的tablayout标签:

   <android.support.design.widget.TabLayout
        android:layout_weight="4"
        android:id="@+id/tabs"
        android:layout_gravity="right"
        app:tabGravity="fill"
        fontPath="@string/fontsIran"
        app:tabTextAppearance="@drawable/selected_text"
        android:layoutDirection="rtl"
        app:tabBackground="@drawable/tabformat"
        app:tabTextColor="@android:color/white"
        app:tabSelectedTextColor="@color/color_tab_gray"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_main"
        />

我只是喜欢根据这篇文章中的javacoder所说的下面的内容,但是我不知道为什么在选择另一个选项卡后上一个文本会被删除

@Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);
                TextView txt=(TextView)findViewById(R.id.tabText);
                txt.setTextColor(Color.GRAY);
                tab.setCustomView(txt);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                TextView txt=(TextView)findViewById(R.id.tabText);
                txt.setTextColor(Color.YELLOW);
                tab.setCustomView(txt);
            }

解决方法:

这是我更改选择的布局的方式.

这是您的适配器

 class ViewPagerAdapter extends FragmentPagerAdapter {
   ...
   // I add two func here.
   public void SetOnSelectView(TabLayout tabLayout,int position)
   {
    TabLayout.Tab tab = tabLayout.getTabAt(position);
    View selected = tab.getCustomView();
    TextView iv_text = (TextView) selected.findViewById(R.id.tabText);
    iv_text.setTextColor(mContext.getResources().getColor(R.color.white));
   }

   public void SetUnSelectView(TabLayout tabLayout,int position)
   {
    TabLayout.Tab tab = tabLayout.getTabAt(position);
    View selected = tab.getCustomView();
    TextView iv_text = (TextView) selected.findViewById(R.id.tabText);
    iv_text.setTextColor(mContext.getResources().getColor(R.color.textblack));
   }
   ...
 }

活动中

 //Add this
 tabLayout.addOnTabSelectedListener(OnTabSelectedListener);

这是OnTabSelectedListener

 private TabLayout.OnTabSelectedListener OnTabSelectedListener = new TabLayout.OnTabSelectedListener(){
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int c = tab.getPosition();
        yourAdapter.SetOnSelectView(tabLayout,c);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        int c = tab.getPosition();
        yourAdapter.SetUnSelectView(tabLayout,c);
    }

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

    }
};

标签:android-tablayout,viewpagerindicator,android-viewpager,android
来源: https://codeday.me/bug/20191026/1936578.html