0
我想將EditText添加到我的操作欄。但是,當我嘗試使用getActionView()搜索該項目時,它始終返回null。getActionView()總是返回null
我剛剛花了5個小時在網上搜索解決方案,但是我發現的所有內容都與SearchView相關,它不適用於我的案例。
看來我需要使用MenuItemCompat.getActionView(),因爲我的minSDK是8,但它總是返回null。
另一種解決方案建議更改proguard規則,但這並沒有幫助。
請幫忙。
menu_email.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="il.co.talkie.q.SettingsEmailActivity">
<item
android:id="@+id/action_save"
android:title="@string/save_action"
android:orderInCategory="100"
app:showAsAction="always"
tools:ignore="AppCompatResource"/>
<item
android:id="@+id/action_err_email"
android:title="@string/email_err_prompt"
android:visible="true"
android:orderInCategory="1"
app:showAsAction="always"
tools:ignore="AppCompatResource"
android:actionLayout="@layout/menu_header"
>
</item>
</menu>
menu_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/simple_blue">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="@string/service_providers_menu_header"
android:layout_weight="1"
android:editable="false"
android:inputType="none" />
</LinearLayout>
,並從該活動的相關代碼
Java代碼
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_email, menu);
MenuItem email_err_menu = menu.findItem(R.id.action_err_email);
View v = (View) MenuItemCompat.getActionView(email_err_menu);
...
return true;
}
上面的物體V總是空,如果你使用的是支持庫
是的。這做到了。非常感謝 – Zvi