编辑文本不会在菜单中膨胀
Edit Text won't inflate in menu
头发都白了,希望大家多多指教。
所以,问题是 EditText 没有膨胀到菜单中,尽管之前和之后的其他项目都正确膨胀。
java代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_notes_optionsmenu, menu);
title_textbox = (EditText)menu.findItem(R.id.title_textbox);
return true;
}
title_textbox 保持为空。通过逐步调试器,我看到在inflation之后,菜单有以下项目:
https://gyazo.com/8a76c0bd702568f4c46a8ef2af1a03c5(图片已链接,因为我没有 10 个代表来嵌入它。)
在back和accept之间,应该有activity_main_notes_optionsmenu.xml中定义的edittext:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menubar">
<item
android:id="@+id/back_button"
android:icon="@drawable/ic_back_button"
android:orderInCategory="1"
android:title="Back"
app:showAsAction="always"/>
<EditText
android:id="@+id/title_textbox"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orderInCategory="2"
app:showAsAction="always"/>
<item
android:id="@+id/accept"
android:icon="@drawable/ic_accept"
android:orderInCategory="3"
android:title="Accept"
app:showAsAction="always"/>
<group android:id="@+id/group1" android:checkableBehavior="none" android:orderInCategory="4">
<item
android:id="@+id/renamenote"
android:icon="@drawable/ic_menu_slideshow"
android:title="Rename Note" />
<item
android:id="@+id/deletenote"
android:icon="@drawable/ic_menu_manage"
android:title="Delete Note" />
</group>
</menu>
我能找到的唯一可行的解决方法是在 android.support.v7.widget.Toolbar 中添加 EditText,但是在这种情况下,orderInCategory 将被忽略,Edittext 将始终位于左侧大多数项目,然后是菜单中的项目。所需的行为是从左到右,我们有后退按钮、EditText、接受按钮,然后是溢出中的最后 2 个 window。
涉及此主题的其他问题是:
- 如上所述的可用解决方法,但由于定位的限制,这不是一个合适的解决方案。
menu.findItem(R.id.*) is null- Android - 这是一个在充气前尝试使用对象的简单案例,不是我的案例。
Android : Display an EditText in a menu - 好像说不可能?尽管根据许多其他消息来源,这是可能的。
Edit Text won't inflate in the action bar - 看起来和我的问题一样,但是在接受的答案中,第一部分是关于铸造的,但我的问题是它一开始就不存在,而关于自定义前缀的部分我尝试过,但没有帮助。
So, the problem is that EditText does not get inflated into the menu, though other items, both before and after, are properly inflated.
因为那从来都不是有效的,所以这并不奇怪。
The only functional workaround I could find, was to add the EditText inside the android.support.v7.widget.Toolbar instead
令我感到有些惊讶的是,它像您所说的那样有效。我原以为 Toolbar
也会忽略随机的非菜单 XML 元素。
欢迎您使用an ActionProvider
or an action view。
头发都白了,希望大家多多指教。
所以,问题是 EditText 没有膨胀到菜单中,尽管之前和之后的其他项目都正确膨胀。
java代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_notes_optionsmenu, menu);
title_textbox = (EditText)menu.findItem(R.id.title_textbox);
return true;
}
title_textbox 保持为空。通过逐步调试器,我看到在inflation之后,菜单有以下项目:
https://gyazo.com/8a76c0bd702568f4c46a8ef2af1a03c5(图片已链接,因为我没有 10 个代表来嵌入它。)
在back和accept之间,应该有activity_main_notes_optionsmenu.xml中定义的edittext:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menubar">
<item
android:id="@+id/back_button"
android:icon="@drawable/ic_back_button"
android:orderInCategory="1"
android:title="Back"
app:showAsAction="always"/>
<EditText
android:id="@+id/title_textbox"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orderInCategory="2"
app:showAsAction="always"/>
<item
android:id="@+id/accept"
android:icon="@drawable/ic_accept"
android:orderInCategory="3"
android:title="Accept"
app:showAsAction="always"/>
<group android:id="@+id/group1" android:checkableBehavior="none" android:orderInCategory="4">
<item
android:id="@+id/renamenote"
android:icon="@drawable/ic_menu_slideshow"
android:title="Rename Note" />
<item
android:id="@+id/deletenote"
android:icon="@drawable/ic_menu_manage"
android:title="Delete Note" />
</group>
</menu>
我能找到的唯一可行的解决方法是在 android.support.v7.widget.Toolbar 中添加 EditText,但是在这种情况下,orderInCategory 将被忽略,Edittext 将始终位于左侧大多数项目,然后是菜单中的项目。所需的行为是从左到右,我们有后退按钮、EditText、接受按钮,然后是溢出中的最后 2 个 window。
涉及此主题的其他问题是:
menu.findItem(R.id.*) is null- Android - 这是一个在充气前尝试使用对象的简单案例,不是我的案例。
Android : Display an EditText in a menu - 好像说不可能?尽管根据许多其他消息来源,这是可能的。
Edit Text won't inflate in the action bar - 看起来和我的问题一样,但是在接受的答案中,第一部分是关于铸造的,但我的问题是它一开始就不存在,而关于自定义前缀的部分我尝试过,但没有帮助。
So, the problem is that EditText does not get inflated into the menu, though other items, both before and after, are properly inflated.
因为那从来都不是有效的,所以这并不奇怪。
The only functional workaround I could find, was to add the EditText inside the android.support.v7.widget.Toolbar instead
令我感到有些惊讶的是,它像您所说的那样有效。我原以为 Toolbar
也会忽略随机的非菜单 XML 元素。
欢迎您使用an ActionProvider
or an action view。