2014-10-09 4 views
0

두 가지 스타일이 있습니다. 각 스타일은 사용자 정의 글꼴 (보통 및 굵게)을 선언합니다.안드로이드 + 스타일을 통해 사용자 정의 글꼴 바꾸기

내 XML에서 기본적으로 글꼴을 표준으로 지정하여 "레이블"스타일을 사용하도록 사용자 지정 TextView를 설정합니다. 그러나 사용자 작업 후에 스타일 label.bold를 통해 굵은 글꼴 버전의 일반 글꼴을 교체하고 싶습니다. 이것이 가능한가? 염두에두고, 내가 스타일을 인수로 사용하지 않는 setTypeface()를 피하기 위해 노력하고 있지만, 쓸데없는 것처럼 보이는 대담한 tff에 대한 포인터.

답변

0

해결되었습니다. 예를 들어 내 사용자 지정 텍스트 뷰에서보세요 :

standardTextView.setTextAppearance(ExampleFragment.this.getActivity(), R.style.label_bold); 

바인드합니다 :

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 
    <declare-styleable name="StandardTextView"> 
     <attr name="typeface" format="string" /> 
    </declare-styleable> 
</resources> 

레이아웃 :

package com.example.ui.components; 

import android.content.Context; 
import android.content.res.AssetManager; 
import android.content.res.Resources; 
import android.content.res.TypedArray; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 

import com.example.core.ui.R; 

public class StandardTextView extends TextView { 

    public StandardTextView(Context context) { 
     this(context, null); 
    } 

    public StandardTextView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public StandardTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     setTypeFaceFromAttributes(attrs.getStyleAttribute()); 
    } 

    @Override 
    public void setTextAppearance(Context context, int resid) { 
     super.setTextAppearance(context, resid); 

     setTypeFaceFromAttributes(resid); 
    } 

    private void setTypeFaceFromAttributes(int attributeSet) { 
     if (attributeSet == -1) { 
      return; 
     } 

     TypedArray a = null; 

     try { 
      a = getContext().obtainStyledAttributes(attributeSet, R.styleable.StandardTextView); 

      if (a.getString(R.styleable.StandardTextView_typeface) == null) { 
       return; 
      } 

      String typeFaceString = a.getString(R.styleable.StandardTextView_typeface); 

      setupTypeface(typeFaceString); 

     } finally { 
      if (a != null) { 

       a.recycle(); 
      } 
     } 
    } 

    private void setupTypeface(String typeFace) { 
     if (typeFace == null || "".equals(typeFace)) { 
      return; 
     } 

     Resources resources = getResources(); 
     if (resources == null) { 
      return; 
     } 

     AssetManager assetManager = resources.getAssets(); 
     if (assetManager == null) { 
      return; 
     } 

     Typeface tf = Typeface.createFromAsset(assetManager, typeFace); 

     setTypeface(tf); 
    } 
} 

이 당신이 그것을 사용할 수있는 방법입니다

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/default_background" 
    android:orientation="vertical" > 

    <com.example.ui.components.StandardTextView 
     android:id="@+id/sure_swipe_create_text" 
     style="@style/label" 
     android:layout_width="match_parent" 
     android:layout_height="56dp" 
     android:gravity="center|top" 
     android:paddingTop="20dp" 
     android:text="@string/example" /> 
</RelativeLayout>