2013-04-15 40 views
0

我正在開發適用於4.0版本的Android應用程序,所有複選框都有黑色背景,但我需要白色。如果我使用「background」xml屬性作爲「白色」,那麼所有複選框(帶有文本)都是白色的;我只需要在白色的地方打勾。如何在Android中爲複選框設置白色背景?

回答

0

最簡單的方法 - 使用ImageView 3種背景狀態(stateSelected=truestateSelected=falsestatePressed = true)仿真CheckBox
只需創建3個背景的合適xml文件並將其設置爲ImageViewbackground
然後在代碼中,當點擊ImageView時只需切換ImageView.setSelected = !ImageView.isSelected

希望它的幫助

0

我不知道如果我是特別遲或什麼,但這裏是solution

代碼片段:

說這個選擇是名「checkbox_selector」

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="true" 
    android:drawable="@drawable/cbchk_blue" 
    android:state_focused="false"> 
</item> 
<item android:state_checked="true" 
    android:drawable="@drawable/cbchk_blue" 
    android:state_focused="true"> 
</item> 
<item android:state_checked="false" 
    android:drawable="@drawable/cbunchk_blue" 
    android:state_focused="false"> 
</item> 
<item android:state_checked="false" 
    android:drawable="@drawable/cbunchk_blue" 
    android:state_focused="true"> 
</item> 

,以便設置只是爲了複選框,而不是整個文本也,你可以把它作爲:

<CheckBox 
    android:layout_width="45dip" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:button="@drawable/checkbox_selector" 
    android:text="@string/text_here" /> 
0

也許我的解決方案是不是很優雅,但它爲我工作:

我simplily創建複選框矩形(15×15)的尺寸,並與白色背景的另一種佈局。然後我將這個佈局添加到一個RelativeLayout中,我也放了這個複選框。有9dp的餘量,我把白色的佈局矩形放在複選框的下面,所以看起來複選框的背景顏色是白色的。

 <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/login_large_margin" 
      android:layout_marginTop="@dimen/login_medium_margin" > 

      <RelativeLayout 
       android:layout_width="15dp" 
       android:layout_height="15dp" 
       android:layout_margin="9dp" 
       android:background="@color/white" > 
      </RelativeLayout> 

      <CheckBox 
       android:id="@+id/login_check" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:fontFamily="sans-serif-light" 
       android:text="@string/login_check" 
       android:textColor="@color/white" > 
      </CheckBox> 
     </RelativeLayout> 
相關問題