我在片段中使用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>
答案 0 :(得分:1)
试试这个:
on onCreateView
tableLayout.getChildAt(0).setSelected(真);
答案 1 :(得分:1)
终于解决了 只需要添加这一行......
tabLayout.getTabAt(0).getCustomView().setSelected(true);
答案 2 :(得分: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>
并将此样式添加到您的tablayout
app:tabTextAppearance="@style/TabLayoutTextAppearance"
答案 3 :(得分: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));
}