我试图删除 MenuItem 填充超过 4 小时,但我自己绝对无法做到这一点。
我尝试过两种方法。第一个是获取 My menuItem ,它的 view 和 setPadding 为 0:
MenuItem back = menu.findItem(R.id.back);
back.setActionView(R.id.testando);
View bView = (View) back.getActionView();
bView.setPadding(0, 0, 0, 0);
在这种情况下的问题是我的 bView 总是返回 NULL,我不知道为什么,因为 back 已经 instatiated...
另一种方法是获取我的 MenuItem 并使用填充 0 设置自定义视图:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.browser_menu, menu);
MenuItem back = menu.findItem(R.id.back);
back.setActionView(R.id.testando);
测试 XML 文件:
<ActionMenuItemView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testando"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:focusable="true"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:paddingStart="0dip"
android:paddingEnd="0dip"
android:textAppearance="?android:attr/actionMenuTextAppearance"
android:textColor="?android:attr/actionMenuTextColor"
style="?android:attr/actionButtonStyle" />
这种情况下的问题是由 ResourcesNotFoundException 引起的
06-17 15:35:39.975: E/AndroidRuntime(9561): FATAL EXCEPTION: main
06-17 15:35:39.975: E/AndroidRuntime(9561): android.content.res.Resources$NotFoundException: Resource ID #0x7f0a0046 type #0x12 is not valid
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2112)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.content.res.Resources.getLayout(Resources.java:858)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
06-17 15:35:39.975: E/AndroidRuntime(9561): at com.android.internal.view.menu.MenuItemImpl.setActionView(MenuItemImpl.java:566)
06-17 15:35:39.975: E/AndroidRuntime(9561): at br.com.facilit.target.app.android.BrowserActivity.onCreateOptionsMenu(BrowserActivity.java:36)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
06-17 15:35:39.975: E/AndroidRuntime(9561): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:388)
06-17 15:35:39.975: E/AndroidRuntime(9561): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
06-17 15:35:39.975: E/AndroidRuntime(9561): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2833)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.os.Handler.handleCallback(Handler.java:605)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.os.Looper.loop(Looper.java:137)
06-17 15:35:39.975: E/AndroidRuntime(9561): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-17 15:35:39.975: E/AndroidRuntime(9561): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:35:39.975: E/AndroidRuntime(9561): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 15:35:39.975: E/AndroidRuntime(9561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-17 15:35:39.975: E/AndroidRuntime(9561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-17 15:35:39.975: E/AndroidRuntime(9561): at dalvik.system.NativeStart.main(Native Method)