1.xml样式文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 角度 -->
    <corners android:radius="2dp"/>
    <!-- 填充色 -->
    <solid  android:color="#ffffff"/>
    <!-- 描边 设置线宽及颜色 -->
    <stroke android:color="@color/colorAccent"
        android:width="1dp"/>
</shape>

2.Search中使用该样式

      <android.support.v7.widget.SearchView
            android:id="@+id/searchView_singer"
            android:layout_width="match_parent"
            android:layout_height="46dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/searviewtest"
            />

3.需求呢,现在是这个描边颜色需要在代码中控制,也就是动态的改变样式颜色,

 

通过 searchView.getBackground() 获取一个 GradientDrawable 对象(如果该 View 设置的是 Shape 背景的话)。
然后通过 drawable.setStroke(1, Color.RED) 动态设置描边就可以了。第一个参数 1 代表是的宽度,第二个是颜色。

        GradientDrawable gradientDrawable1= (GradientDrawable) searchView.getBackground();
        gradientDrawable1.setStroke(3, xColor);
        searchView.setBackground(gradientDrawable1);

  

相关文章:

  • 2021-08-09
  • 2021-11-19
  • 2021-05-20
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2021-04-08
  • 2021-06-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-08-25
  • 2022-03-02
  • 2023-04-10
  • 2021-12-18
相关资源
相似解决方案