GradientDrawable類的利用動態設置樣式中的顏色


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);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM