java - 如何在工具栏中间添加编辑文本?

标签 java android android-layout android-edittext toolbar

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

我尝试将编辑文本放入 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" />

最佳答案

在工具栏中添加编辑文本,请尝试以下代码

    <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>

关于java - 如何在工具栏中间添加编辑文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854837/

相关文章:

java - 从 Android 中的 URL 解析极其简单的 JSON

java - 在 Java 中读取二进制文件

java - Soundcloud Java Api包装程序重复文件错误

android - 当电池电量不足时,Android Oreo 不会收到 FCM 通知

android - 让一个元素填充基于wrap_content的布局中的所有可用空间

java - Netbeans IDE 不发出有关在构造函数中调用的方法的警告

android - 动态添加项目到 RecyclerView

android - 由于android studio中缺少com.android.application插件,我的应用无法构建?

android - RadioButton 将文本左对齐,按钮右对齐

android - 当我从垂直 View 更改为水平 View 时,为什么我的歌曲会重新开始播放?