2014-02-27 3 views
2

중첩 된 preferencesScreens를 만들었습니다. 사용자 정의 글꼴을 추가하고 싶습니다. 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() 함수를 사용하여 자식 preferenceScreen에 환경 설정을 추가했다는 것을 알립니다. CustomPreference를 사용할 때이 메서드를 사용할 수 없습니다. 그래서 하위 화면에 환경 설정을 추가 할 수 없습니다. 그래서 내가 무엇을해야하니? –

+0

@KelumDeshapriya :'addPreferencesFromResource (R.xml.preference)'를 사용하고 있지 않습니까? –

+0

예. 나는 그것을 사용한다. 하지만 난 또한 addPreference() 프로그램에서 자식 preferenceScreen 내 동적 환경 설정 목록을 추가 할 수 있습니다. –