gpt4 book ai didi

android - 如何在自定义 tablayout 中更改 tablayout 选定的文本颜色

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:53 26 4
gpt4 key购买 nike

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

最佳答案

这是我更改选中的 tablayout 的方法。

这是你的适配器

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

Activity 中

 //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 中更改 tablayout 选定的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com