简体   繁体   English

在工具栏左侧添加搜索图标

[英]add search icon to the left of toolbar

I want to achieve rtl toolbar and for that I use the tricky way . 我想实现rtl工具栏,为此我使用了棘手的方法。 for that i inflate this menu into the toolbar . 为此,我将此菜单添加到工具栏中。 but now i want add search icon in left of the toolbar . 但是现在我想在工具栏的左侧添加搜索图标。 also i set the getSupportActionbar.setTitle(""); 我也设置了getSupportActionbar.setTitle(""); for removing title . 用于删除标题。

main.xml: main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/title"
        android:orderInCategory="100"
        android:title="My Title"
        app:showAsAction="always"/>
    <item
        android:id="@+id/icon"
        android:orderInCategory="100"
        android:title=""
        android:icon="@drawable/ic_menu"
        app:showAsAction="always"
        />

</menu>

this is the toolbar screenshot from my toolbar and i mark the place for the search icon exactly . 这是我工具栏中的工具栏屏幕截图,我准确标记了搜索图标的位置。

在此处输入图片说明

Put this in your menu file.hope this will work 把它放到你的菜单文件中。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search / will display always -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/action_search"
          android:title="search"
          android:showAsAction="ifRoom"
          android:actionViewClass="android.widget.SearchView"/>
</menu>

From Android API Level 17+ it supports RTL natively. 从Android API级别17+开始,它原生支持RTL。 To force your entire layout to be RTL including the ActionBar do the following. 若要将整个布局强制为包括ActionBar的RTL,请执行以下操作。

Edit your AndroidManifest.xml and add android:supportsRtl="true" to your <application> tag and then add the following line to the top of your Activities' onCreate() method forceRTLIfSupported(); 编辑您的AndroidManifest.xml并将android:supportsRtl =“ true”添加到您的<application>标记,然后将以下行添加到“活动”的onCreate()方法的顶部:forceRTLIfSupported(); and then insert the follow function into your Activity. 然后将Follow函数插入到Activity中。

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported()
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }
}

Of course that's only helpful for debugging. 当然,这仅对调试有用。 Use View.LAYOUT_DIRECTION_LOCALE for production builds so your layout is only changed when the user chosen system location/language aka locale supports RTL. View.LAYOUT_DIRECTION_LOCALE用于生产版本,以便仅当用户选择的系统位置/语言(也称为语言环境)支持RTL时,才更改布局。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM