本文介绍了如何在 Android 中更改选定的选项卡标题 textSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一种方法来更改选中时 Tab Title
的文本大小.直到现在没有出口.希望有人可以帮助我.
I am trying to find a way to change text size of Tab Title
when selected. Until now without exit. Hope someone can help me.
我的代码如下:
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tab_layout"
app:tabSelectedTextColor="@android:color/holo_orange_dark"
app:tabTextAppearance="@style/textAppearance">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager_view"/>
tabTextAppearance 使用的样式:
Style used for tabTextAppearance:
<style name="textAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
</style>
我的适配器:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final Resources resources;
private static final int num = 3;
public ViewPagerAdapter(FragmentManager fm, Resources resources) {
super(fm);
this.resources = resources;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break;
}
return fragment;
}
@Override
public int getCount() {
return num;
}
@Override
public CharSequence getPageTitle(int position) {
String title = null;
switch (position) {
case 0:
title = "A";
break;
case 1:
title = "B";
break;
case 2:
title = "C";
break;
}
return title;
}
}
还有我的轮播片段类:
public class CarouselFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_carousel, container, false);
tabLayout = (TabLayout)root.findViewById(R.id.tab_layout);
viewPager = (ViewPager)root.findViewById(R.id.pager_view);
setHasOptionsMenu(true);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), getResources());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setSelectedTabIndicatorColor(Color.RED);
tabLayout.setupWithViewPager(viewPager);
}else {
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//here i should do something, but what???
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//here i should do something, but what???
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
非常感谢!
推荐答案
使用这段代码
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
TextView tabTextView = new TextView(this);
tab.setCustomView(tabTextView);
tabTextView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
tabTextView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
tabTextView.setText(tab.getText());
if (i == 0) {
tabTextView.setTextSize(16);
}
}
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
ViewGroup vgTab = (ViewGroup) vg.getChildAt(tab.getPosition());
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTextSize(16);
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
ViewGroup vgTab = (ViewGroup) vg.getChildAt(tab.getPosition());
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTextSize(14);
}
}
}
}
这篇关于如何在 Android 中更改选定的选项卡标题 textSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!