【问题标题】:Android: How to create EditText field at the bottom of PopupMenu?Android:如何在 PopupMenu 底部创建 EditText 字段?
【发布时间】:2015-12-25 19:55:32
【问题描述】:

我的代码中有一个 PopupMenu 视图,我希望 EditText 视图成为它的选项之一,以便用户可以从他们选择现有文件的同一菜单中创建一个新文件。根据这篇文章:Android : Display an EditText in a menu 不能将 EditText 视图用作 PopupMenu 项。

我的问题是,是否有解决方法无法将 EditText 声明为菜单项?也许,创建一个 ListView 并将 Popupmenu 和 EditText 视图声明为子视图?

下面是我尝试过的代码,但正如预测的那样,生成了一个没有可见 EditText 字段的 Popupmenu。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <item android:id="@+id/new_track"
        android:title="+ create new track"/>

    <EditText
        android:id="@+id/search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search_hint"
        android:inputType="textCapWords"
        android:imeOptions="actionSend" />

</menu>

我建议的代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ListView

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <item android:id="@+id/new_track"
        android:title="+ create new track"/>
</menu>
<EditText
        android:id="@+id/search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search_hint"
        android:inputType="textCapWords"
        android:imeOptions="actionSend" />

</ListView>

最好的方法是什么?

【问题讨论】:

    标签: android xml menu


    【解决方案1】:

    我认为您需要创建RelativeLayout,然后设置每个元素需要进入队列的位置。

            <?xml version="1.0" encoding="utf-8"?>
            <menu xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content">
    
            <item android:id="@+id/new_track"
                  android:title="+ create new track"/>
    
        <RelativeLayout>
        <EditText
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/search_hint"
            android:inputType="textCapWords"
            android:imeOptions="actionSend"
            android:layout_alignParentBottom="true"/> //this line put edittext at bottom
        </RelativeLayout>
    </menu>
    

    编辑:在多次搜索这个特定问题后,我只能说:不,你不能那样做。您只能创建一个菜单 item,它会调用特定的片段,或者它将您的工具栏更改为 SearchView(如隐藏 TextView、显示 EditText 和 Buttons)。

    对于第一个选项,请查看:Search item on action bar android

    对于第二个选项:http://blog.rhesoft.com/2015/03/30/tutorial-android-actionbar-with-material-design-and-search-field/

    【讨论】:

    • 我试过了,但 EditText 小部件仍然没有显示
    • 我的应用在全屏模式下运行,所以没有应用栏。我使用了一种解决方法来解决我的问题,在我的弹出菜单中单击一个选项以创建新文件会更改用户可以在其中命名文件的单独 EditText 字段的可见性。
    • 无论如何,我都会给你正确的答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2012-04-02
    • 2014-10-18
    相关资源
    最近更新 更多