Android 如何在工具栏中添加带有编辑文本的搜索栏
我想在工具栏中添加带有编辑文本的搜索栏,如下图所示 My toolbar.xml:-Android 如何在工具栏中添加带有编辑文本的搜索栏,android,android-toolbar,Android,Android Toolbar,我想在工具栏中添加带有编辑文本的搜索栏,如下图所示 My toolbar.xml:- <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
只需在工具栏中添加编辑文本,如下所示:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:elevation="4dp">
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
我相信您正在寻找的视图是一个搜索视图
。您可以在菜单.xml
上定义要将其添加到操作栏/工具栏的内容。像这样:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/search_icon"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
这是一种更简单的方法。最明智的方法是实际进行一项活动来响应您的搜索意图。有关这方面的更多信息,请访问:据我所知,我希望此代码可以帮助您
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_500"
android:orientation="vertical"
android:gravity="center|right">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
android:orientation="horizontal"
android:gravity="center|right">
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="8"
android:layout_margin="5dp"
android:text="snap deal"
android:gravity="center" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:layout_margin="5dp"
android:ems="10"
android:text="Img"
android:gravity="center" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
android:gravity="center|right">
<EditText
android:id="@+id/editMobileNo"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="@drawable/login_edittext"
android:ems="10"
android:hint="Find your dil ki deal"
android:drawableLeft="@drawable/search"
android:gravity="center" >
</EditText>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
这只是草图。。。您需要更改背景颜色、大小、图像等…您可以直接将searchview放置在工具栏内的activity.xml中并对其进行初始化。我想您将获得所需的布局。为了向后兼容,我喜欢上面的文本编辑解决方案
这里有两个要考虑的事情来实现原来的例子:
在活动xml文本编辑定义中添加:
android:drawableStart=“@android:drawable/ic_菜单_搜索”
android:hint=“@string/Search_Txt”
在字符串xml文件上添加:
string name=“Search_Txt”>查找您的dil-ki交易
// Customize your message here.
注:SearchView替代方案需要API 21
享受。以及如何在编辑文本右侧添加搜索图像图标将其添加为常规项目菜单。您的问题已完全改变。你现在想要的是什么,看看链接。不,我的问题是一样的,我想要的是我上面给出的图片的搜索框,我不是在谈论开发方面,我只是想知道如何设计我的工具栏,就像我给出的snap deal images一样你想要的图片是第一张,但你得到的是第二张,实际上你想要的是第一张图片。我说得对吗?好的。。也许我明白了。。。请稍候您的android:background=“@drawable/login\u edittext”您能给我这些文件吗?这个工作正常,但搜索栏会覆盖其他图标并显示工具栏顶部。edittext是snap deal的顶部吗?是的edittext是snap deal的顶部吗!!!。。我使用线性布局。你复制的代码和我贴的完全一样吗。。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:endColor="@color/white"
android:startColor="@color/white" />
<stroke
android:width="1dp"
android:color="@color/gray_500" />
<corners
android:radius="1dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</selector>
// To add the magnifying glass.
// To add the hint message.
// Customize your message here.