Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android操作栏中设置SearchView的样式_Android_Search_Styles_Searchview - Fatal编程技术网

在Android操作栏中设置SearchView的样式

在Android操作栏中设置SearchView的样式,android,search,styles,searchview,Android,Search,Styles,Searchview,我的操作栏中有一个搜索小部件,如下所示: (1) 如何更改显示“iPhone”的文本的颜色 (2) 另外,如果你注意到灰色的X——当它处于图标位置时,整个搜索小部件也是这种颜色。我在Holo.Theme.Light上,并利用我自己的mods来实现它 如何在我的styles.xml文件中更改小部件的这两种样式(假设您在这里更改搜索小部件)?我解决这个问题的方法是使用Holo.Light.DarkActionBar主题。我不相信您可以轻松地更改搜索小部件,否则。您必须设置 searchView.s

我的操作栏中有一个搜索小部件,如下所示:

(1) 如何更改显示“iPhone”的文本的颜色

(2) 另外,如果你注意到灰色的X——当它处于图标位置时,整个搜索小部件也是这种颜色。我在Holo.Theme.Light上,并利用我自己的mods来实现它


如何在我的styles.xml文件中更改小部件的这两种样式(假设您在这里更改搜索小部件)?

我解决这个问题的方法是使用Holo.Light.DarkActionBar主题。我不相信您可以轻松地更改搜索小部件,否则。

您必须设置

searchView.setBackgroundColor(Color.WHITE);

搜索视图的可定制性不强。

您可以更改搜索视图的样式。下面是我用来调整编辑文本、语音图标的一些代码

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        searchView.findViewById(searchPlateId).setBackgroundResource(R.drawable.textfield_search_selected);

        int voiceSearchPlateId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
        searchView.findViewById(voiceSearchPlateId).setBackgroundResource(R.drawable.textfield_search_right_selected);

        // change hint color
        int searchTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView searchTextView = (TextView) searchView.findViewById(searchTextViewId);
        searchTextView.setHintTextColor(getResources().getColor(R.color.light_grey));

[编辑]这里有一个更完整的答案:

如果您使用的是appcompat库,那么解决方案与Jerome的答案有点不同。这是我的解决办法

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.main_menu, menu);
    restoreActionBar();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setHintTextColor(Color.WHITE);
    searchAutoComplete.setTextColor(Color.WHITE);

    View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light);

    ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchCloseIcon.setImageResource(R.drawable.clear_search);

    ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
    voiceIcon.setImageResource(R.drawable.abc_ic_voice_search);

    ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    searchIcon.setImageResource(R.drawable.abc_ic_search);


    return super.onCreateOptionsMenu(menu);
}

我为此花了很多时间,但最后::-)

  • 要更改文本颜色,请执行以下操作:
  • 或者这个是AndroidX的:

    ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
    
    ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
    
  • 要更改文本提示,请执行以下操作:
  • 或者这个是AndroidX的:

    ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
    
    ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
    

    如果您想要白色文本,则应扩展具有黑色背景的操作栏主题,如
    Holo.theme
    theme.AppCompat.Light.darkaActionBar
    (如果您使用的是支持库)。

    无需其他设置。

    onCreateOptions菜单中:

    int id = searchView.getContext()
                       .getResources()
                       .getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextColor(Color.WHITE);
    
    现在,这是用于更改搜索栏板颜色的xml文件searchcolor.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@+id/search"
              android:icon="@drawable/search"
              android:showAsAction="ifRoom"
              android:actionViewClass="android.widget.SearchView" />
    
    </menu>
    
    
    
    和menu/menu.xml,用于在操作栏中创建搜索

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@+id/search"
              android:icon="@drawable/search"
              android:showAsAction="ifRoom"
              android:actionViewClass="android.widget.SearchView" />
    
    </menu>
    

    从棒棒糖开始,您可以在博客文章中设置这样的搜索视图样式

    values/themes.xml:
    @样式/MySearchViewStyle
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    
    在xxxActivity.java文件中获取SearchView,然后:

    SearchView searchView = (SearchView)menu.findItem(R.id.my_search_view).getActionView();
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    
    // Getting the 'search_plate' LinearLayout.
    View searchPlate = searchView.findViewById(searchPlateId);
    searchPlate.setBackgroundResource(R.drawable.textfield_searchview);
    
    创建文件
    res/drawable/texfield\u searchview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true"
            android:drawable="@drawable/textfield_search_selected_holo_light" />
        <item android:drawable="@drawable/textfield_search_default_holo_light" />
    </selector>
    
    
    
    1。图标位置意味着什么?2.在(2)中,你真正想要改变什么?X的颜色?下划线的颜色?两者都有?嘿,这是个老问题,我解决了——有点。我把答案写在下面。我主要想改变文本和X的颜色。在一个图标的位置只是放大镜在行动栏。你点击它,它就会像你上面看到的那样打开。这是一个肮脏的黑客,但它work@KickingLettuce你能告诉我你使用定制的actionbar布局吗?如何在左边显示图标图像?终于!!在Xamarin.Android@Pongpat Thanks上运行非常完美。实际上,您可以在
    SearchView
    中设置
    EditText
    的文本颜色。但是,正如您所提到的,这并不像使用XML样式定义那么容易。您甚至可以设置下划线的颜色。查看其他关于
    SearchView
    customization的信息。我认为这是最好的方法。非常有趣,需要尝试一下。几周前我确实解决了这个问题。。。但知道这一点很好。向上投票。我怎样才能找到这些ID?我想将放大镜图标和“x”更改为深色。+1。这对我有帮助。但是,在ABS的情况下,您可以只使用ID,而不是使用getIdentifier<代码>searchView.findViewById(R.id.abs\u search\u plate).setBackgroundResource(R.drawable.myBackground)类似地,您可以使用提交区域和搜索src文本,因为Alsow是懒惰的,不喜欢以下链接。请在回答中包含解决方案,而不仅仅是一个链接。其中大部分内容来自appcompat中的abc_search_view.xml文件。希望有人能对此有所帮助。这正是我所需要的。你能在哪里真正更改
    search\u mag\u图标?我可以改变一切,除了这个。有没有一种方法可以使用搜索视图前棒棒糖的样式?例如,像KitKat?它是AppCompat——用于棒棒糖前的设备。
    
    SearchView searchView = (SearchView)menu.findItem(R.id.my_search_view).getActionView();
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    
    // Getting the 'search_plate' LinearLayout.
    View searchPlate = searchView.findViewById(searchPlateId);
    searchPlate.setBackgroundResource(R.drawable.textfield_searchview);
    
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true"
            android:drawable="@drawable/textfield_search_selected_holo_light" />
        <item android:drawable="@drawable/textfield_search_default_holo_light" />
    </selector>
    
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
    searchView.setSubmitButtonEnabled(true); // to enable submit button
    
    ImageView b = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
    b.setImageResource(android.R.drawable.ic_menu_search); //to change submit button icon
    
    TextView a=(TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    a.setTextColor(...); //to change color of search text