【问题标题】:Icon for SearchView in ActionBar vom AppCompat always has a shadowActionBar vom AppCompat 中 SearchView 的图标总是有阴影
【发布时间】:2014-02-20 12:20:41
【问题描述】:

我在 AppCompat Actionbar 中添加了一个 SearchView,但它的图标总是有阴影。

对于我使用the Android Downloads page 中没有阴影的其他图标,实际上我也尝试为SearchViewandroid:icon="@drawable/ic_action_search" 分配一个图标,但这不会改变图标。

我的研究表明,实际上来自android-support-v7-appcompat.jarab_ic_search.png 正在被使用,而我的android:icon="@drawable/ic_action_search" 被忽略了。

我尝试了各种方法,例如尝试替换onPrepareOptionsMenu 中的图像(通过尝试查找ImageView)或通过SearchView 的自定义子类,但无济于事。

任何想法如何解决这个问题?基本上我只想让我的图标统一,我是否必须为所有其他图标添加阴影?可能我无论如何都无法为那些“三个点”添加阴影?

【问题讨论】:

    标签: android android-actionbar searchview android-appcompat


    【解决方案1】:

    感谢this blogpost,我设法构建了我的解决方案:

    public boolean onCreateOptionsMenu(Menu menu) {
        // the search view is in the mymenu.xml
        inflater.inflate(R.menu.mymenu, menu);
    
        // now that it is inflated we can get the item
        MenuItem item = menu.findItem(R.id.action_find);
    
       // and through it the actionview
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        Drawable searchIcon = getActivity().getResources().getDrawable(R.drawable.ic_action_search); // the new icon
    
        // and now we search within the view for the "bad" image
        ImageView collapsedSearchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    
        // and finally replace it
        collapsedSearchIcon.setImageDrawable(searchIcon);
    }
    

    【讨论】:

    • 要获取扩展搜索图标,请使用 id android.support.v7.appcompat.R.id.search_mag_icon
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2015-04-28
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多