在使用shape的同時,用代碼修改shape的顏色屬性

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

Android裡面經常會使用shape來定製一些View的背景

可以修改View的背景顏色,形狀等屬性


一般情況下,shape都是在xml檔案裡面寫死了,今天遇到一個需求,View的形狀是圓角的,但是顏色是在代碼裡面設定的

最開始的思路是先在代碼裡給View設定顏色,再在shape裡面設定solid屬性為透明色

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <corners        android:topLeftRadius="8dp"        android:topRightRadius="8dp"        android:bottomLeftRadius="8dp"        android:bottomRightRadius="8dp"/>        <solid        android:color="#00000000"/></shape>

View.setBackgroundColor(color);View.setBackgroundDrawable(R.drawable.shape);

很遺憾,不能實現我想要的需求,每設定一次Background,Background就會就會被替換掉


最後上網Google了下,找到解決方案了http://stackoverflow.com/questions/16775891/how-to-change-solid-color-from-the-code

GradientDrawable myGrad = (GradientDrawable)view.getBackground();myGrad.setColor(color);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.