nAn*_*oid 5 fonts android androiddesignsupport
如何使用书法将自定义字体应用于设计支持库中的TabLayout?
我已经得到它在Java工作,大多数答案似乎参考.(例如,在Android设计支持TabLayout中更改选项卡文本的字体)
我不想做自定义课程,我只想使用书法.(https://github.com/chrisjenx/Calligraphy)
谢谢
小智 8
我使用扩展TabLayout的方法.通过这种方式,您可以在TabLayout中简单地定义fontPath;
<com.mypackage.base.widget.FontAwareTabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
fontPath="@string/common_fonts_sans_condensed_bold"
/>
Run Code Online (Sandbox Code Playgroud)
这里的类代码:
/**
* Simple helper class which extends a TabLayout to allow us to customize the font of the tab.
* https://gist.github.com/tmtrademarked/09926077a406959be15fc8a824a52751
* https://github.com/chrisjenx/Calligraphy/issues/180
*/
public final class FontAwareTabLayout extends TabLayout {
private String fontPath;
public FontAwareTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
fontPath = pullFontPathFromView(context, attrs, new int[] { R.attr.fontPath });
}
/**
* Tries to pull the Custom Attribute directly from the TextView.
*
* @param context Activity Context
* @param attrs View Attributes
* @param attributeId if -1 returns null.
* @return null if attribute is not defined or added to View
*/
static String pullFontPathFromView(Context context, AttributeSet attrs, int[] attributeId) {
if (attributeId == null || attrs == null) return null;
final String attributeName;
try {
attributeName = context.getResources().getResourceEntryName(attributeId[0]);
} catch (Resources.NotFoundException e) {
// invalid attribute ID
return null;
}
final int stringResourceId = attrs.getAttributeResourceValue(null, attributeName, -1);
return stringResourceId > 0 ? context.getString(stringResourceId)
: attrs.getAttributeValue(null, attributeName);
}
@Override
public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
super.addTab(tab, position, setSelected);
ViewGroup mainView = (ViewGroup) getChildAt(0);
ViewGroup tabView = (ViewGroup) mainView.getChildAt(tab.getPosition());
int tabChildCount = tabView.getChildCount();
for (int i = 0; i < tabChildCount; i++) {
View tabViewChild = tabView.getChildAt(i);
if (tabViewChild instanceof TextView) {
CalligraphyUtils.applyFontToTextView(getContext(), (TextView) tabViewChild, fontPath);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 4
有一种方法可以在 TabLayout 的 xml 中使用自定义字体,但它有点 hacky。您必须为选项卡提供自己的自定义布局,并且在该布局中您可以为 TextView 设计任何您喜欢的样式。
所以基本上你需要这样的设置:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SampleFragmentPagerAdapter pagerAdapter =
new SampleFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
然后是 PagerAdapter:
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
private Context context;
private String tabTitles[] = new String[] { "Tab1", "Tab2" };
// ...
public View getTabView(int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView
View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) v.findViewById(R.id.textView);
tv.setText(tabTitles[position]);
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
这是custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
fontPath="fonts/CustomFont.otf"
tools:ignore="MissingPrefix" />
Run Code Online (Sandbox Code Playgroud)
代码中缺少一些东西,但我认为你可以填写缺少的部分,这只是它的要点。这只是参考文献中博客文章的一部分,并添加了书法。您可以查看它以了解更多详细信息。
参考: