2011-01-23 18 views
29

Tôi cần thay đổi màu đột quỵ từ ứng dụng. Người dùng có thể thay đổi màu nền vì vậy tôi cũng cần cho phép họ thay đổi nét vẽ (đường viền) của nút. Vì nó đã được thiết lập trong drawable (mẫu dưới đây) tôi đã không tìm thấy một cách để thay đổi này. Có vẻ như tất cả các câu hỏi khác như thế này chỉ được dùng để sử dụng tệp XML .... nhưng điều đó không cho phép tôi làm cho nó động. Cảm ơn bạn đã giúp đỡ!Tôi cần thay đổi màu stroke thành màu do người dùng xác định. Không liên quan gì đến tiểu bang

Tôi cần thay đổi màu stroke thành màu do người dùng xác định. Không có gì để làm với nhà nước.

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 

    <solid android:color="#ffffffff"/>  
     <stroke 
       android:width="3dp" 
       android:color="@color/Dim_Gray" /> <<<<--- This is what I need to change 


    <padding android:left="10dp" 
      android:top="10dp" 
      android:right="10dp" 
      android:bottom="10dp" 
      /> 

    <corners android:bottomRightRadius="12dp" android:bottomLeftRadius="12dp" 
    android:topLeftRadius="12dp" android:topRightRadius="12dp"/> 

</shape> 

Trả lời

1

Có lẽ họ đang đề cập đến Color State Lists cho phép bạn thay đổi màu sắc dựa trên việc nút được nhấn/tập trung/kích hoạt/etc

+0

Xin lỗi tôi didnt giải thích nó đủ tốt. Tôi cần thay đổi màu stroke thành màu do người dùng xác định. không có gì để làm với nhà nước. –

+0

Thử sử dụng shapeDrawable (http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html) trong đó Paint có Style = Stroke, StrokeWidth = , Color = f20k

0

Hãy thử sử dụng StateLists (như trái ngược với ColorStateList). Hãy xem: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Bạn cũng có thể tạo ra một ShapeDrawable (hoặc một RoundRectShape trong ví dụ của bạn) theo chương trình, và sau đó gọi của nút setBackgroundDrawable

+0

Xin lỗi tôi đã không giải thích nó đủ tốt.Tôi cần thay đổi màu stroke thành màu do người dùng xác định. không có gì để làm với nhà nước. –

0

tôi đã trả lời một câu hỏi tương tự trong Change shape border color at runtime

của nó như cùng một giải pháp được đề xuất bởi f20k nhưng trong trường hợp của tôi, drawable là một GradientDrawable thay vì một ShapeDrawable.

xem nếu nó hoạt động ...

96

1. Nếu bạn có tập tin drawable cho một "cái nhìn" như thế này

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

<corners android:radius="5dp" /> 

<solid android:color="@android:color/white" /> 

<stroke 
    android:width="3px" 
    android:color="@color/blue" /> 

</shape> 

Sau đó, bạn có thể thay đổi
a. màu Stroke:

GradientDrawable drawable = (GradientDrawable)view.getBackground(); 
drawable.setStroke(3, Color.RED); // set stroke width and stroke color 


b. màu rắn:

GradientDrawable drawable = (GradientDrawable)view.getBackground(); 
drawable.setColor(Color.RED); // set solid color 

2. Nếu bạn có tập tin drawable cho một "cái nhìn" như thế này

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:id="@+id/buttonSelected"> 
     <shape> 
      <solid android:color="@color/blue" /> 
      <stroke android:width="1px" android:color="@color/blue" /> 
     </shape> 
    </item> 
    <item android:state_checked="false" android:id="@+id/buttonNotSelected"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/white" /> 
      <stroke android:width="1px" android:color="@color/blue" /> 
     </shape> 
    </item> 
</selector> 

Sau đó, bạn có thể thay đổi các thuộc tính cá nhân mục bằng cách lấy đối tượng drawable riêng bởi các vị trí đó.

StateListDrawable drawable = (StateListDrawable)view.getBackground(); 
DrawableContainerState dcs = (DrawableContainerState)drawable.getConstantState(); 
Drawable[] drawableItems = dcs.getChildren(); 
GradientDrawable gradientDrawableChecked = (GradientDrawable)drawableItems[0]; // item 1 
GradientDrawable gradientDrawableUnChecked = (GradientDrawable)drawableItems[1]; // item 2 

nay đến thay đổi đột quỵ hoặc màu rắn:

//solid color 
gradientDrawableChecked.setColor(Color.BLUE); 
gradientDrawableUnChecked.setColor(Color.RED); 

//stroke 
gradientDrawableChecked.setStroke(1, Color.RED); 
gradientDrawableUnChecked.setStroke(1, Color.BLUE); 
+0

Tổng quan tuyệt vời về cách làm việc với bộ chọn và hình dạng theo chương trình. – Mercury

+0

Câu trả lời hoàn hảo. Làm việc như một say mê. – Marlon

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