Tablayout文字颜色不会改变,Android吗?

问题描述 投票:1回答:4

我正在使用TabLayout的片段。这是我的标签主要片段

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

public TabFragment () {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_tab, container, false);
    viewPager = (ViewPager) v.findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) v.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
    tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));

    return v;
}

private void setupTabIcons() {
    TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null);
    tabOne.setText("\"fragment 1\"");
    tabLayout.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null);
    tabTwo.setText("\"fragment 2\"");
    tabLayout.getTabAt(1).setCustomView(tabTwo);
}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new Fragment1(), "fragment 1");
    adapter.addFragment(new Fragment2(), "fragment 2");
    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);
    }
}
}

片段XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.TabFragment">

<!-- TODO: Update blank fragment layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabBackground="@drawable/tab_color_selector"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:layout_margin="10dp"
        android:background="@drawable/tab_rectangle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

</FrameLayout>

选项卡的文本视图

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:text="XXXX"
android:textColor="@color/tab_text_color"
android:gravity="center"
android:textSize="18sp"
android:layout_height="wrap_content" />

颜色选择器-tab_text_color

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

打开应用程序后,我没有看到所选标签文本为白色。

这是选项卡选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorAppBlue" android:state_selected="true"/>
<item android:drawable="@android:color/white"/>
</selector>
android colors tabs textview
4个回答
1
投票

尝试此:

on onCreateView

tableLayout.getChildAt(0).setSelected(true);


1
投票

最终解决了只需添加此行即可。...

tabLayout.getTabAt(0).getCustomView().setSelected(true);

0
投票

[也许,您的textColor被android覆盖或丢失了我看不到的代码中的某些内容。但是,我将为您提供另一种使用此方法来更改tablayout文本颜色的方法。

<style name="TabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">//your text color</item>
    <item name="android:textAllCaps">true</item>
</style>

并将此样式添加到您的布局中>

app:tabTextAppearance="@style/TabLayoutTextAppearance"

0
投票
You can try this for changing the tab background color or text color


LinearLayout tabsContainer = (LinearLayout) tabLayout.getChildAt(0);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
                    LinearLayout item = (LinearLayout) tabsContainer.getChildAt(section - 1);
                    TextView tv = (TextView) item.getChildAt(1);
                    item.setBackgroundColor(getResources().getColor(R.color.color00DF4C));
                    tv.setTextColor(getResources().getColor(R.color.colorWhite));
                }
© www.soinside.com 2019 - 2024. All rights reserved.