Android 为什么我的自定义布局充气机工厂onCreateView从未被超越?
我已经创建了这个自定义布局充气器工厂类,并按照文档中的建议将其设置为布局充气器的克隆。应用程序运行,但从未在我的自定义类上调用onCreateView 通过研究LayoutInflater.java,我可以看到应该从createViewFromTag调用它,但是很难跟踪在哪种情况下执行此方法 我试图自定义的菜单是Action Overflow菜单 你知道会出什么问题吗 我的定制充气机工厂等级:Android 为什么我的自定义布局充气机工厂onCreateView从未被超越?,android,layout-inflater,Android,Layout Inflater,我已经创建了这个自定义布局充气器工厂类,并按照文档中的建议将其设置为布局充气器的克隆。应用程序运行,但从未在我的自定义类上调用onCreateView 通过研究LayoutInflater.java,我可以看到应该从createViewFromTag调用它,但是很难跟踪在哪种情况下执行此方法 我试图自定义的菜单是Action Overflow菜单 你知道会出什么问题吗 我的定制充气机工厂等级: public class MyLayoutInflaterFactory implements Lay
public class MyLayoutInflaterFactory implements LayoutInflater.Factory {
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name.equalsIgnoreCase(
"com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
view.setBackgroundResource(R.drawable.banner_kid_yellow_blue);
((TextView) view).setTextSize(20);
Typeface face = MyApplication.getDefaultTypeface();
((TextView) view).setTypeface(face);
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (InflateException e) {
//Handle any inflation exception here
} catch (ClassNotFoundException e) {
//Handle any ClassNotFoundException here
}
}
return null;
}
}
我的OnCreateMenuOptions方法:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
LayoutInflater inflaterClone = getLayoutInflater().cloneInContext(getLayoutInflater().getContext());
LayoutInflater.Factory lif = new MyLayoutInflaterFactory();
inflaterClone.setFactory(lif);
return super.onCreateOptionsMenu(menu);
}
谢谢大家! 请在声明onCreateView的公共类之前添加@Override onCreateView仅在使用工厂设置的充气器对象对布局充气时调用 视图应在Activity的设置内容视图之前膨胀 在您的情况下,可以在设置ContentView之前放大菜单布局,并存储自定义标记数据以供将来使用 在setContentView()之前创建时在中编写代码
LayoutInflater充气机=LayoutInflater.from(此);
if(充气器.getFactory()!=null){
充气器=充气器克隆文本(本);
}
充气器。设置工厂();
视图v=充气机。充气(,空);
希望能有帮助 您必须从activity onCreateView中手动调用factory onCreateView。因为活动的onCreateView在默认情况下返回null,所以如果您需要其他方面的帮助,可以这样做
@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if(name.contains("ActionMenuItemView")) {
LayoutInflater li = LayoutInflater.from(context);
View view = null;
try {
view = li.createView(name, null, attrs);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (view != null) {
if(factory != null) {
view = mFactory.onCreateView(name,context,attrs);
}
return view;
}
}
return super.onCreateView(name, context, attrs);
}
这将检查LayoutFlater是否可以创建视图,然后触发factory onCreateView对其进行编辑
@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if(name.contains("ActionMenuItemView")) {
LayoutInflater li = LayoutInflater.from(context);
View view = null;
try {
view = li.createView(name, null, attrs);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (view != null) {
if(factory != null) {
view = mFactory.onCreateView(name,context,attrs);
}
return view;
}
}
return super.onCreateView(name, context, attrs);
}