2014-10-09 27 views
0

我有两种样式,每种都声明自定义字体(普通和粗体)。Android +通过样式交换自定义字体

<style name="label" parent="@android:style/Widget.TextView"> 
     <item name="typeface">@string/custom_font_regular</item> 
    </style> 

    <style name="label.bold"> 
     <item name="typeface">@string/custom_font_semibold</item> 
    </style> 

在我的xml中,我将自定义TextView设置为使用“标签”样式,默认情况下将字体样式设置为正常。但是,在用户操作之后,我想通过style label.bold替换粗体版本的普通字体。这可能吗?请记住,我试图避免调用setTypeface(),它不会将样式作为参数,而是指向大胆的tff,这似乎很浪费。

回答

0

已解决。在我的自定义的TextView看一看为例:

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); 
    } 
} 

这是如何使用它:

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

ATTRS:

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

布局:

<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>