gpt4 book ai didi

java - 如何在 Android 中更改选定的选项卡标题 textSize

转载 作者:搜寻专家 更新时间:2023-11-01 07:45:58 26 4
gpt4 key购买 nike

我正在尝试找到一种方法来在选择时更改 Tab Title 的文本大小。到现在没有导出。希望有人能帮助我。

我的代码如下:

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

还有我的 Carousel Fragment 类:

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

关于java - 如何在 Android 中更改选定的选项卡标题 textSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244469/

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