2015-05-21 205 views
0

我想將我的操作欄的背景設置爲漸變顏色。爲此我有一個Colorpicker,我可以在其中選擇開始和結束顏色。我想用這些值來設置背景。將背景設置爲漸變顏色

但我不知道int值應該設置爲可以繪製到操作欄的漸變格式。起初我認爲格式應該是這樣的:#FFFFFFF就像我在那種情況下使用的那樣:actionbar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(hexFarbe)));例如。

我這個試了一下:

if (x > 10 && x < 138 && y > 316 && y < 356){ 
      endfarbe_zuletzt_gewählt_global = false; 
      startfarbe_zuletzt_gewählt_global = true; 
      String hexColor = String.format("#%06X", (0xFFFFFF & startFarbe)); 
      Log.d("startfarbe", "startfarbe " + endFarbe + "|" + hexColor); 
      startFarbe_global = hexColor; 
      mListener.colorChanged("startFarbe", startFarbe); 
     } 

在這裏,我想設置的動作條顏色:

if(!(startFarbe.equalsIgnoreCase(""))&&(!(endfarbe.equalsIgnoreCase("")))){ 
      GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM, 
        // new int[] {0xFF616261,0xFF131313}); 
        new int[] {Integer.parseInt(startFarbe), Integer.parseInt(endfarbe)}); 

      actionbar.setBackgroundDrawable(gd); 
} 

但後來我得到以下logcat的錯誤:

Caused by: java.lang.NumberFormatException: Invalid int: "#0000FF" 
+0

Android使用ARGB,你可以嘗試創建一個類似顏色「 #AARRGGBB」 –

回答

3

你在您提供的字符串中獲得NumberFormatException作爲# g不能由parseInt()方法處理。

當用顏色字符串的工作,而不是Integer.parseInt(),嘗試Color類的方法:

public static int parseColor (String colorString) 

Android documentation

Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'