我尝试实现自定义菜单。我使用了 this 中给出的答案问题。在我的代码中,名称是 ExpandedMenuItem,但在所有示例中它都是 IconMenuItemView。那里发生了什么?我该如何纠正这个问题?
这是我的代码。
public class MyActivity extends PreferenceActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.proximity_alert_menu, menu);
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
//if(name.equalsIgnoreCase("com.android.internal.view.menu.MenuItem")) {}
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
TextView tView = (TextView) view;
tView.setTypeface(Config.set_font);
tView.setTextColor(Color.RED);
}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
}
异常表明
java.lang.ClassCastException:com.android.internal.view.menu.ExpandedMenuView cannot be cast to android.widget.TextView
我如何将其转换为 TextView?
最佳答案
您可以如下检查 View
是否为 TextView
...
if (view instanceof TextView) {
TextView tView = (TextView) view;
tView.setTypeface(Config.set_font);
tView.setTextColor(Color.RED);
}
如果 View 是 TextView
,则 Font
和 Color
将被更改。
关于android - 在 Android 中为菜单项设置自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22610924/