【问题标题】:How to add an edittext in the middle of the toolbar?如何在工具栏中间添加一个edittext?
【发布时间】:2019-09-09 13:16:49
【问题描述】:

我想将编辑文本直接添加到工具栏。编辑文本始终显示在工具栏下方或上方。我能做什么?

我试图将编辑文本放在 app_bar_nav.xml 中,但这对它的位置没有帮助。我还尝试了一个包含语句并将布局类型更改为线性布局......也没有一个好的结果。

app_bar_nav.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorBlack"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlack"
        android:hint="Adresse" />

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    app:srcCompat="@android:drawable/ic_menu_search" />

<include layout="@layout/content_nav" />

activity_nav.xml

<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">


<include
    layout="@layout/app_bar_nav"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_nav"
    app:menu="@menu/activity_nav_drawer" />

【问题讨论】:

    标签: java android android-layout android-edittext toolbar


    【解决方案1】:

    在 Toolbar 中自行添加编辑文本,请尝试以下代码

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorBlack"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
    
        <EditText
            android:id="@+id/myEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorBlack"
            android:hint="Adresse" />
        </androidx.appcompat.widget.Toolbar>
    

    【讨论】:

    • 很好,请将此答案标记为正确答案可能是其他人面临相同问题。
    【解决方案2】:

    您可以将搜索用作菜单

    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>
    </menu>
    

    在Activity/Fragment中访问

    public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater menuInflater = getMenuInflater();
       menuInflater.inflate(R.menu.dashboard, menu);
       MenuItem searchItem = menu.findItem(R.id.action_search);
       SearchManager searchManager = (SearchManager)MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
       SearchView searchView = null;
       if (searchItem != null) {
        searchView = (SearchView) searchItem.getActionView();
       }
       if (searchView != null){
           searchView.setSearchableInfo( searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
       }
       return super.onCreateOptionsMenu(menu);
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 SearchView 代替 EditText。

      将此代码添加到xml中

      <SearchView
          android:id="@+id/simpleSearchView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:iconifiedByDefault="false"
          android:queryHint="Search View"
          android:background="#f00"
          android:padding="40dp"/>
      

      这里是how

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多