如何在Android中更改EditTextPreference的dialogMessage的字体?
我想更改对话框的默认字体样式,但我不知道怎么做!
这是我的自定义EditTextPreference:
public class MyEditTextPreferences extends EditTextPreference {
public MyEditTextPreferences(Context context) {
super(context);
}
public MyEditTextPreferences(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditTextPreferences(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
titleView.setTypeface(G.yekanfont);
summaryView.setTypeface(G.yekanfont);
}
}
和此:
<com.mydomain.mypackagename.MyEditTextPreferences
android:defaultValue="300"
android:dialogMessage="some text"
android:dialogTitle="some other text"
android:inputType="number"
android:key="DURATION_BETWEEN"
android:title="some text"
android:summary="some other text"
/>
答案 0 :(得分:0)
您需要在onCreateView中执行此操作。像这样:
@Override
protected View onCreateView(ViewGroup viewGroup) {
View v = super.onCreateView(viewGroup);
try {
TextView titleView = (TextView) v.findViewById(android.R.id.title);
titleView.setTypeface(G.yekanfont);
TextView summaryView = (TextView) v.findViewById(android.R.id.summary);
summaryView.setTypeface(G.yekanfont);
} catch (Exception e) {
Log.i("TAG", "Excpetion ", e);
}
return v;
}
答案 1 :(得分:0)
您不需要使用自定义 EditTextPreference 类。只需执行以下操作:
EditTextPreference preferenceEditText = (EditTextPreference) findPreference(itsKey);
EditText editText = preferenceEditText.getEditText();
//Now do whatever you want with this EditText
要更改标题和摘要的字体,请在您的 styles.xml
中执行以下操作:
<style name="PreferenceTheme" parent="@style/AppTheme">
...
<item name="android:fontFamily">@font/lato</item>
<item name="fontFamily">@font/lato</item>
</style>
不确定哪一个有效,所以我只输入了 android:fontFamily
和 fontFamily
。要应用样式,请转到您的首选项片段类:
private ViewGroup parent;
...
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
parent = container;
...
}
...
@Override
public void onResume() {
super.onResume();
...
parent.getContext().setTheme(R.style.PreferenceTheme);
}