将EditText添加到工具栏 [英] Add EditText to Toolbar
问题描述
我的Toolbar
和EditText
看起来像这样:
我尝试将其添加为ActionView
,但得到了以下结果:
I tried adding it as an ActionView
but I got this result:
它基本上只是添加了我的单词在下面点击"作为标题,而没有TextView
It basically just added my words 'Tap Below' as a title without the TextView
这就是我所做的:
menu.xml
<menu>
...
...
<item
android:id="@+id/edit_text_menu"
android:orderInCategory="300"
android:title="Tap Below"
android:actionLayout="@layout/edit_text_layout"
app:showAsAction="ifRoom|collapseActionView"
/>
</menu>
edit_text_layout.xml
edit_text_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myEditText"
android:background="#000000">
</EditText>
我认为通过这样做,我可以将EditText
布局添加到Toolbar
...
有人可以帮忙吗?
I thought by doing this I add my EditText
layout to the Toolbar
...
Can someone please help?
或者,有什么技巧可以将EditText
覆盖在Toolbar
上吗?
Alternatively, is there a trick where I can overlay the EditText
on the Toolbar
?
我尝试添加<item name="windowActionModeOverlay">true</item>
,但是它什么也没做.
I tried adding <item name="windowActionModeOverlay">true</item>
but it didn't do anything.
推荐答案
只需将EditText
添加到ToolBar
的XML中即可.那可能是最简单的方法.
Just add the EditText
to the XML for your ToolBar
. That is probably the easiest way.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myEditText"
android:background="#000000" />
</android.support.v7.widget.Toolbar>
这篇关于将EditText添加到工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!