2013-06-19 62 views
0

我正在開發與版本17(與Galaxy Nexus)爲Android的用戶界面,並且,我得到了背景白色。當我在版本15(與Galaxy II)中嘗試相同的代碼時,背景是黑色的。那麼,如果代碼相同,爲什麼顏色不同呢?背景顏色回舊Android版本

另一個問題是,我正在使用TextView的函數setBackground,它只適用於版本16或更新版本。它是setBackgroundResourse的等價物嗎?

這是我製作代碼的一個例子。

FrameLayout frame = new FrameLayout(this); 
     frame.setId(findId()); 
     FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);    
     frame.setBackgroundResource(idBorder); 
     frame.setLayoutParams(frameParams); 


     //Relative layaout que engloba todo 
     RelativeLayout relativeLayout = new RelativeLayout(this); 
     //relativeLayout.setId(findId()); 
     RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
     relativeLayout.setLayoutParams(relativeParams);   
     relativeLayout.setPadding(5, 5, 5, 5); 
     relativeParams.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     relativeParams.addRule(RelativeLayout.CENTER_VERTICAL); 
     relativeLayout.setBackgroundColor(Color.WHITE); 
     //relativeLayout.setBackground(getResources().getDrawable(idBorder)); 
     relativeLayout.setBackgroundResource(idBorder); 
     frame.addView(relativeLayout); 

     //Nombre de la sala 
     TextView textRoomName = new TextView(this); 
     textRoomName.setId(findId()); 
     relativeLayout.addView(textRoomName); 

     textRoomName.setText(room.getName()); 
     RelativeLayout.LayoutParams relativeParamRoomName = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
     relativeParamRoomName.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
     relativeParamRoomName.setMargins(10, 10, 10, 5);  
     textRoomName.setLayoutParams(relativeParamRoomName); 
     textRoomName.setTextColor(Color.parseColor("#040404")); 
     textRoomName.setTextSize(20); 
     textRoomName.setTypeface(null, Typeface.BOLD); 
     textRoomName.setPadding(3, 3, 3, 3);   
     //textRoomName.setBackground(getResources().getDrawable(idBorder)); 
     textRoomName.setBackgroundResource(idBorder); 

enter image description here enter image description here

+0

它是因爲您使用的主題 –

+0

我可以在哪裏檢查它?我試圖刪除邊框比我使用和定義getBackgroundColor,它的工作更好一點。如果這是主題,我可以開發代碼,以免出現問題併成爲最可能的標準嗎? – Guille

回答

0

你可以看到應用程序清單文件和style.xml使用的是什麼主題。請參閱this以保持兼容性。

+0

你確定嗎?我正在檢查它..好吧,我有版本15和17,它不應該有太大的區別。我使用Theme.Light和Holo。這很奇怪,那是改變RelativeLayaout或TextViews的背景顏色 – Guille

+0

我已經將min和sdkversion更改爲15,並且它繼續發生,我猜如果這是因爲主題它現在應該工作。 – Guille