【问题标题】:How to change the toolbar SearchView Underline color to GRAY如何将工具栏 SearchView 下划线颜色更改为灰色
【发布时间】:2017-02-07 15:17:06
【问题描述】:

当我们展开搜索栏时,我正在尝试更改搜索视图的下划线颜色

    SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
    autoCompleteTextView.setCursorVisible(true);
    autoCompleteTextView.setTextColor(ContextCompat.getColor(this, R.color.textcolor_light_gray));
    autoCompleteTextView.setHintTextColor(autoCompleteTextView.getTextColors().withAlpha(255));
    autoCompleteTextView.setTextSize(16);

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

这里我也用主题来改变颜色

 <style name="MySearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">@color/darkgray</item>

</style>

但没有什么对我有用。请任何人帮助我...

【问题讨论】:

    标签: android xml toolbar android-toolbar searchview


    【解决方案1】:

    终于找到解决办法了..

        View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
        searchplate.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
    

    【讨论】:

    • 我们可以通过 xml 做同样的事情吗?主题或风格?
    • 没有。我只尝试了主题或样式。但它不起作用。所以只有我被添加了滤色器
    • @sree getBackground() 返回 null
    • @ArturA,我们需要在使用getBackground()之前设置背景。这里我使用了 searchplate.setBackgroundResource(R.drawable.autocomplete_bg);
    • 必须添加 PorterDuff.Mode.SRC_ATOP 以丢弃下划线的原始基色并使用新的。
    【解决方案2】:

    你也可以通过xml来改变它:

    <item name="android:queryBackground">...</item>
    

    我猜你忘记了android: 前缀

    【讨论】:

      【解决方案3】:

      下面的示例对我有用,我希望它也对你有用。

      <SearchView
          android:id="@+id/search_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:iconifiedByDefault="false"
          android:gravity="center"
          android:background="@drawable/bg_searchview"
          android:queryBackground="@drawable/bg_searchview_query"/>
      

      android:queryBackground="@drawable/bg_searchview_query"

      在drawable中,为背景创建.xml文件bg_searchview

      <?xml version="1.0" encoding="utf-8"?>
      <shape
          xmlns:android="http://schemas.android.com/apk/res/android">
          <padding
              android:top="10dp"
              android:bottom="10dp" />
          <solid android:color="#FFFFFF" />
          <corners android:radius="20dp"/>
          <stroke
              android:width="0.3dp"
              android:color="#797979" />
      </shape>
      

      在drawable中,为queryBackground创建一个.xml文件bg_searchview_query

      <?xml version="1.0" encoding="utf-8"?>
      <shape
          xmlns:android="http://schemas.android.com/apk/res/android">
          <solid android:color="#FFFFFF" />
          <corners android:radius="20dp"/>
      </shape>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        • 2012-09-15
        • 1970-01-01
        • 2019-07-05
        • 1970-01-01
        • 2018-09-11
        • 2016-04-04
        相关资源
        最近更新 更多