Hello Everyone我正在开发一个标签式应用程序,其中我为应用程序徽标和tablayout保留了工具栏,其中包含选项卡名称,我想更改这些选项卡名称中的字体,但我无法&# 39;因为名称不是实际的文本视图,所以我想出来了,我在论坛和其他地方搜索得很好但是找不到任何人回答这个问题,所以简单地说我想要的是更改这些选项卡名称中的字体是我的代码,谢谢你的帮助!
public class MainActivity extends AppCompatActivity {
public Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(4);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//here i set the fragments to use i have separate fragment.java
// for each page i show that refers to another xml that shows the content
//the words between "" are the tabs name and i need to change their fonts :(
adapter.addFragment(new FiveFragment(),"ثقافة");
adapter.addFragment(new FourFragment(), "فن");
adapter.addFragment(new ThreeFragment(), "تعليم");
adapter.addFragment(new TwoFragment(), "تاريخ");
adapter.addFragment(new OneFragment(), "علوم");
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
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/logo"
android:src="@drawable/icon"
android:layout_width="60dp"
android:layout_height="58dp"
android:layout_marginRight="323dp"
android:layout_marginEnd="330dp"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabTextColor="#000"
app:tabTextAppearance="@style/MineCustomTabText"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#0ff"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
答案 0 :(得分:1)
您可以为TabLayout设置自定义样式以更改字体。
在styles.xml
:
<style name="AppTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/YourTextAppareance</item>
</style>
然后设置YourTextAppareance
样式。
<style name="YourTextAppareance" parent="TextAppearance.Design.Tab">
<item name="android:fontFamily">sans-serif</item>
</style>
最后,将样式设置为TabLayout:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
style="@style/AppTabLayout"
app:tabTextColor="#000"
app:tabTextAppearance="@style/MineCustomTabText"
app:tabGravity="fill"/>
另一种方法是使用自定义视图并在自定义视图的布局中设置字体。但这是一种更复杂的方法,因为您必须在“选择”和“未选择”时处理视图。
public class SectionsPageAdapter extends FragmentPageViewAdapter {
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public View getCustomView(int position, View v, boolean selected) {
if (v == null) {
LayoutInflater li = LayoutInflater.from(App.getApplication());
v = li.inflate(R.layout.view_tab, null);
}
TextView textView = (TextView) v.findViewById(R.id.textView);
// Change your font on the textview here.
textView.setText(getPageTitle(position));
// Take in account the selected var if you want another layout when
// tab is selected.
return v;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
if (position == 0)
return Fragment.newInstance();
else if (position == 1)
return Fragment.newInstance();
else
return Fragment.newInstance();
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab 1";
case 1:
return "Tab 2";
case 2:
return "Tab 3";
}
return null;
}
}
然后,juste浏览选项卡列表并添加自定义视图。
for (int i=1; i < mSectionsPagerAdapter.getCount(); i++)
{
tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, null, false));
}
不要忘记检查onPageSelected更改以更新视图:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < tabLayout.getTabCount(); i++) {
if (i == position)
tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, tabLayout.getTabAt(i).getCustomView(), true));
else
tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, tabLayout.getTabAt(i).getCustomView(), false));
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
对于提醒,这里是如何以编程方式更改字体:
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/DejaVuSerif-Italic.ttf");
newfont.setTypeface(font);