나는 TabLayout
안드로이드 디자인 라이브러리에서 새로운 작업을 시도하고 있습니다.
탭 텍스트를 사용자 지정 글꼴 로 변경하고 싶습니다 . 그리고와 관련된 스타일링을 좀 찾아 보려고 TabLayout
했지만 결국 이것으로 끝났습니다 .
탭 텍스트 글꼴을 변경하는 방법을 안내하십시오.
답변
이와 같은 Java 코드 또는 XML에서 TextView를 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:textSize="15sp"
android:textColor="@color/tabs_default_color"
android:gravity="center"
android:layout_height="match_parent"
/>
사용자 정의 textview를 사용하는 경우 TabLayout이이 ID를 확인하므로 ID를 그대로 유지하십시오.
그런 다음 코드에서이 레이아웃을 확장하고 Typeface
해당 텍스트보기 에서 사용자 지정 을 설정 하고이 사용자 지정보기를 탭에 추가합니다.
for (int i = 0; i < tabLayout.getTabCount(); i++) {
//noinspection ConstantConditions
TextView tv = (TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
tv.setTypeface(Typeface);
tabLayout.getTabAt(i).setCustomView(tv);
}
답변
사용 TabLayout
중이고 글꼴을 변경하려면 다음과 같이 이전 솔루션에 새로운 for 루프를 추가해야합니다.
private void changeTabsFont() {
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(Font.getInstance().getTypeFace(), Typeface.NORMAL);
}
}
}
}
답변
자신 만의 스타일을 만들고 부모 스타일을 parent="@android:style/TextAppearance.Widget.TabWidget"
그리고 탭 레이아웃에서이 스타일을 app:tabTextAppearance="@style/tab_text"
예 : 스타일 :
<style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:fontFamily">@font/poppins_regular</item>
</style>
예 : 탭 레이아웃 구성 요소 :
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabTextAppearance="@style/tab_text" />
답변
praveen Sharma의 훌륭한 답변입니다. 약간의 추가 : changeTabsFont()
필요한 모든 곳 에서 사용하는 대신 TabLayout
자신 만의 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(), "fonts/Roboto-Regular.ttf");
}
@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);
}
}
}
}
그리고 하나 더.
TabView
A는 LinearLayout
로 TextView
내부 (또한 선택적으로 포함 할 수있다 ImageView
). 따라서 코드를 더 간단하게 만들 수 있습니다.
@Override
public void addTab(Tab tab) {
super.addTab(tab);
ViewGroup mainView = (ViewGroup) getChildAt(0);
ViewGroup tabView = (ViewGroup) mainView.getChildAt(tab.getPosition());
View tabViewChild = tabView.getChildAt(1);
((TextView) tabViewChild).setTypeface(mTypeface, Typeface.NORMAL);
}
하지만이 방법은 권장하지 않습니다. 경우 TabLayout
구현이 변경됩니다,이 코드가 제대로 작동하지 않거나 충돌 할 수 있습니다.
사용자 정의하는 또 다른 방법은 사용자 TabLayout
정의보기를 추가하는 것입니다. 여기에 좋은 예가 있습니다.
답변
(API 레벨 16) 이상을 XML
실행하는 장치 에서 기능에서 글꼴 지원을 Android 4.1
사용하려면 지원 라이브러리 26 이상을 사용하십시오.
- res 폴더를 마우스 오른쪽 버튼으로 클릭
- 새로 만들기-> Android 리소스 디렉토리-> 글꼴 선택-> 확인
myfont.ttf
새로 만든 글꼴 폴더에 파일을 넣으십시오.
에 res/values/styles.xml
추가 :
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/myfont</item>
</style>
레이아웃 파일에서 app : tabTextAppearance = “@ style / customfontstyle”을 추가합니다.
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabTextAppearance="@style/customfontstyle"
app:tabMode="fixed" />
[fonts in xml]을 참조하세요 . ( https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml )
답변
다음 방법은 전체 글꼴을 ViewGroup
재귀 적으로 변경 합니다. 의 내부 구조에 신경 쓸 필요가 없기 때문에이 방법을 선택했습니다 TabLayout
. 내가 사용하고 서예 글꼴을 설정하는 라이브러리를.
void changeFontInViewGroup(ViewGroup viewGroup, String fontPath) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (TextView.class.isAssignableFrom(child.getClass())) {
CalligraphyUtils.applyFontToTextView(child.getContext(), (TextView) child, fontPath);
} else if (ViewGroup.class.isAssignableFrom(child.getClass())) {
changeFontInViewGroup((ViewGroup) viewGroup.getChildAt(i), fontPath);
}
}
}
답변
디자인 지원 23.2.0의 경우 setupWithViewPager를 사용하여 코드를 addTab (Tab tab)에서 addTab (Tab tab, boolean setSelected)로 이동해야합니다.