gpt4 book ai didi

java - 在选项卡标题上设置 Android 自定义字体

转载 作者:行者123 更新时间:2023-12-01 18:00:52 26 4
gpt4 key购买 nike

我试图从下面的代码中为标题(“选项卡项目 1”、“选项卡项目 2”、“选项卡项目 3”)设置自定义字体。我对 Android Studio 和 Java 非常陌生,只是边做边学,并通过互联网获取了一些资料。

假设我的资源文件夹中已有名为“abc.ttf”的字体,如何更改选项卡标题的字体?下面是我的 TabFragment.java

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TabFragment extends Fragment {

TabLayout mTabLayout;
ViewPager mViewPager;
private static final int int_items = 3;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tabbed_layout, null);
mTabLayout = (TabLayout) x.findViewById(R.id.tabs);
mViewPager = (ViewPager) x.findViewById(R.id.viewpager);

/**
*Set an Apater for the View Pager
*/
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/

mTabLayout.post(new Runnable() {
@Override
public void run() {
mTabLayout.setupWithViewPager(mViewPager);
}
});

return x;

}

class MyAdapter extends FragmentPagerAdapter {

public MyAdapter(FragmentManager fm) {
super(fm);
}

/**
* Return fragment with respect to Position .
*/

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TabOneFragment();
case 1:
return new TabTwoFragment();
case 2:
return new TabThreeFragment();
}
return null;
}

@Override
public int getCount() {

return int_items;

}

/**
* This method returns the title of the tab according to the position.
*/



@Override
public CharSequence getPageTitle(int position) {

switch (position) {
case 0:

return "Tab Item 1";
case 1:
return "Tab Item 2";
case 2:
return "Tab Item 3";
}
return null;
}
}

}

这是我的 tabbed_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="@color/white"
app:tabIndicatorColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v4.view.ViewPager>


</LinearLayout>

最佳答案

创建一个从 Tablayout 驱动的 CustomTabLayout 类,然后在 xml 中使用此 CustomTablayout 而不是 >标签布局

import android.content.Context;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CustomTabLayout extends TabLayout {
private Typeface mTypeface;

public CustomTabLayout(Context context) {
super(context);
init();
}

public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
mTypeface = Typeface.createFromAsset(getContext().getAssets(), "your fonts"); // here you will provide fully qualified path for fonts
}

@Override
public void addTab(Tab tab) {
super.addTab(tab);

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) {
((TextView) tabViewChild).setTypeface(mTypeface, Typeface.NORMAL);
}
}
}

}

这就是您必须在 xml 中执行的操作。

<yourpackagename.CustomTabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="@color/white"
app:tabIndicatorColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

编辑

这个自定义类

package com.ifavaz.kulli;

/**
* Created by ifawaz on 12/11/16.
*/

import android.content.Context;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CustomTabLayout extends TabLayout {
private Typeface mTypeface;

public CustomTabLayout(Context context) {
super(context);
init();
}

public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
mTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/AmazingDayEveryday.ttf"); // here you will provide fully qualified path for fonts
}

@Override
public void setupWithViewPager(ViewPager viewPager) {
super.setupWithViewPager(viewPager);

if (mTypeface != null) {
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);

PagerAdapter adapter = viewPager.getAdapter();

for (int i = 0, count = adapter.getCount(); i < count; i++) {
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(mTypeface, Typeface.NORMAL);
}
}
}

}

这是工作截图enter image description here

希望这对你有帮助

关于java - 在选项卡标题上设置 Android 自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41073895/

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