2010-09-17 42 views
34

tôi muốn thay đổi màu nền của nút bằng cách sử dụng bộ chọn-xml-file. Cách tiếp cận của tôi về cơ bản là một từ ví dụ ở phía dưới trang này: http://developer.android.com/guide/topics/resources/color-list-resource.htmlNút Android với các màu nền khác nhau

tôi có một res/màu/button_text.xml mà trông như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:color="#ffff0000"/> <!-- pressed --> 
    <item android:state_focused="true" 
      android:color="#ff0000ff"/> <!-- focused --> 
    <item android:color="#ff000000"/> <!-- default --> 
</selector> 

và bố trí của tôi chứa đoạn mã sau:

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/button_text" 
    **android:background="@color/button_text"** /> 

(** chỉ có để cho bạn thấy rằng tôi sử dụng android: background thay vì android: Format)

mã này bị treo. nó nói "thẻ nhị phân XML dòng # 4 thẻ yêu cầu 'drawable' thuộc tính hoặc thẻ con xác định drawable.Nhưng nếu tôi thử nó với android: textColor như mô tả trong liên kết ở trên nó hoạt động tốt.Vì vậy, nó phải là vấn đề nền. không muốn tạo 9patch-png nếu nó không cần thiết (về cơ bản tôi chỉ cần một hình chữ nhật "có thể nhấp" để tôi sử dụng nút có nền màu)

+0

câu trả lời Konstantin của có một lỗi đánh máy - dòng 'mục android: color =" @ color/black "' .......... Tôi đề xuất nên đọc mục android: drawable = "@ color/black" ........... –

+0

http: // stackoverflow. com/a/15880688/1352919 – Faakhir

Trả lời

77

Vì lỗi của bạn, bạn phải xác định drawable attibute cho các mục (đối với một số lý do nó là cần thiết khi nói đến khái niệm nền), vì vậy:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed --> 
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused --> 
    <item android:drawable="@color/black"/> <!-- default --> 
</selector> 

Cũng lưu ý rằng drawable att ribute không chấp nhận các giá trị màu nguyên, vì vậy bạn phải xác định màu sắc làm tài nguyên. Tạo colors.xml hồ sơ tại res/values ​​ thư mục:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="black">#000</color> 
    <color name="blue">#00f</color> 
    <color name="red">#f00</color> 
</resources> 
+5

mục android: color = "@ color/black" s/b mục android: drawable = "@ color/black" – Noah

+0

@Noah thx, sửa lỗi đó. –

+0

Nếu điều này giải quyết được vấn đề của bạn, hãy chấp nhận câu trả lời này như một câu trả lời? –

4

Bạn phải đặt các tập tin trong thư mục selector.xml drwable. Sau đó viết: android:background="@drawable/selector". Điều này sẽ chăm sóc các trạng thái nhấn và tập trung.

27

Trong URL bạn đã trỏ đến, button_text.xml đang được sử dụng để đặt thuộc tính textColor.Đó là lý do chúng có button_text.xml trong thư mục res/color và do đó chúng đã sử dụng @ color/button_text.xml

Nhưng bạn đang cố gắng sử dụng nó cho thuộc tính nền. Thuộc tính background tìm kiếm một cái gì đó trong thư mục res/drawable.

séc i này có này nút tùy chỉnh chọn từ internet.I không có link.but tôi cảm ơn những tấm áp phích cho this.It giúp me.have này trong thư mục drawable

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
     <shape> 
      <gradient 
       android:startColor="@color/yellow1" 
       android:endColor="@color/yellow2" 
       android:angle="270" /> 
      <stroke 
       android:width="3dp" 
       android:color="@color/grey05" /> 
      <corners 
       android:radius="3dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 

    <item android:state_focused="true" > 
     <shape> 
      <gradient 
       android:endColor="@color/orange4" 
       android:startColor="@color/orange5" 
       android:angle="270" /> 
      <stroke 
       android:width="3dp" 
       android:color="@color/grey05" /> 
      <corners 
       android:radius="3dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 

    <item>   
     <shape> 
      <gradient 
       android:endColor="@color/white1" 
       android:startColor="@color/white2" 
       android:angle="270" /> 
      <stroke 
       android:width="3dp" 
       android:color="@color/grey05" /> 
      <corners 
       android:radius="3dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 

</selector> 

Và tôi sử dụng trong bố cục main.xml của tôi như thế này

<Button android:id="@+id/button1" 
      android:layout_alignParentLeft="true" 
      android:layout_marginTop="150dip" 
      android:layout_marginLeft="45dip" 
      android:textSize="7pt" 
      android:layout_height="wrap_content" 
      android:layout_width="230dip" 
      android:text="@string/welcomebtntitle1" 
      android:background="@drawable/custombutton"/> 

Hy vọng điều này sẽ hữu ích. Vik là chính xác.

EDIT: Đây là colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="yellow1">#F9E60E</color> 
    <color name="yellow2">#F9F89D</color> 
    <color name="orange4">#F7BE45</color> 
    <color name="orange5">#F7D896</color> 
    <color name="blue2">#19FCDA</color> 
    <color name="blue25">#D9F7F2</color> 
    <color name="grey05">#ACA899</color> 
    <color name="white1">#FFFFFF</color> 
    <color name="white2">#DDDDDD</color> 
</resources> 
+0

bạn có tệp color.xml để đi cùng với điều này không? – electrichead

+0

@electrichead - Có tôi có nó. Tôi đang chỉnh sửa câu trả lời của mình. xin vui lòng xem. – Sreeram

+0

Lần đầu tiên tôi nhìn thấy câu trả lời này khi tôi bắt đầu làm việc với Android và tôi đã bị tấn công bởi số lượng mã XML, nhưng hai tháng sau tôi lại gặp lại nó và đó là câu trả lời tuyệt vời cho vấn đề chung về tạo màu nút tùy chỉnh , không sử dụng hình ảnh. –

2

trong Mono Android bạn có thể sử dụng bộ lọc như thế này:

your_button.Background.SetColorFilter(new Android.Graphics.PorterDuffColorFilter(Android.Graphics.Color.Red, Android.Graphics.PorterDuff.Mode.Multiply)); 
+0

Nền phải đọc getbackground(). Một tùy chọn khác là sử dụng bộ lọc như vậy: myButton.getBackground(). SetColorFilter (new LightingColorFilter (multiplier, bckgrd_color)); – hornetbzz

Các vấn đề liên quan