2014-02-27 72 views
2

我已經創建了嵌套的preferenceScreens.I想添加自定義字體到preferenceScreen title and summary.我嘗試使用加載到字體的字體。我怎樣才能做到這一點? 謝謝。添加字體到preferenceScreen標題

這裏是我preference.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
     <PreferenceCategory android:title="@string/manage_device_title" 
     android:key="@string/device_category"> 
      <PreferenceScreen android:title="@string/manage_device" 
       android:key="@string/manage_device_KEY" 
       android:summary="@string/device_summary" >  
      </PreferenceScreen> 
     </PreferenceCategory> 
    </PreferenceScreen> 

回答

5

您需要創建爲CustomPreferenceCustomPreferenceCategory。包括在preference.xml

CustomPreference CustomPreferenceCustomPreferenceCategory

public class CustomPreference extends Preference { 
Typeface fontStyle; 

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

} 

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

public CustomPreference(Context context) { 
    super(context); 
} 

@Override 
protected void onBindView(View view) { 
    super.onBindView(view); 

    fontStyle = Typeface.createFromAsset(CA.getApplication().getApplicationContext().getAssets(), AppConstants.fontStyle); 
    TextView titleView = (TextView) view.findViewById(android.R.id.title); 
    titleView.setTypeface(fontStyle); 
    titleView.setTextColor(Color.RED); 
    TextView summaryView = (TextView) view.findViewById(android.R.id.summary); 
    summaryView.setTypeface(fontStyle); 
    summaryView.setTextColor(Color.RED); 
    } 
} 

CustomPreferenceCategory:

public class CustomPreferenceCategory extends PreferenceCategory { 
Typeface fontStyle; 

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

} 

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

public CustomPreferenceCategory(Context context) { 
    super(context); 
} 

@Override 
protected void onBindView(View view) { 
    super.onBindView(view); 

    fontStyle = Typeface.createFromAsset(CA.getApplication() 
      .getApplicationContext().getAssets(), AppConstants.fontStyle); 
    TextView titleView = (TextView) view.findViewById(android.R.id.title); 
    titleView.setTypeface(fontStyle); 
    // titleView.setTextColor(Color.RED); 
     } 
} 

在你Preference.xml你需要創建PreferenceCategoryPreference與這些自定義類。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <CustomPreferenceCategory android:title="@string/manage_device_title" 
    android:key="@string/device_category"> 
     <CustomPreference android:title="@string/manage_device" 
      android:key="@string/manage_device_KEY" 
      android:summary="@string/device_summary" >  
     </CustomPreference> 
    </CustomPreferenceCategory> 
</PreferenceScreen> 

注:preference.xml refereing時使用CustomPerenceCategory和CustomPreference適當的包名和fontStyle加按的需要。

+0

謝謝你的回答。對不起,我已經在我的代碼中使用preferenceScreen.addPreference()函數向child preference screen添加了首選項。當我使用CustomPreference時,此方法不可用。所以我不能將偏好添加到子屏幕。所以我該怎麼做? –

+0

@KelumDeshapriya:你不是在使用'addPreferencesFromResource(R.xml.preference)'嗎? –

+0

你好。我使用它。但我也使用addPreference()將我的動態首選項列表添加到程序中的child preferenceScreen。 –