2012-04-10 20 views

Trả lời

33

tạo một file xml trong thư mục dawable của bạn đặt tên giống như gradient_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="#000" 
     android:endColor="#fff" 
     android:angle="90" 
    /> 
</shape> 

và đặt làm nền cho View.

android:background="@drawable/gradient_bg" 

hoặc

setBackgroundResource(R.drawable.gradient_bg); 
+0

Tôi muốn xác định chiều cao của đầu màu sắc bạn có thể nói làm thế nào để làm gì? – mohan

+2

nó là không thể, tuy nhiên bạn có thể chỉ định 'android: centerColor =" # fff "' cũng có, và bạn cũng có thể xác định mảng màu sắc lập trình, trong đó bạn có thể có nhiều hơn ba màu. ví dụ [xem tại đây] (http://stackoverflow.com/a/6116273/593709). –

4
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

    <gradient 

     android:angle="90" 
     android:type="linear" 
     android:startColor="#FFFFFF" 
     android:endColor="#000000" /> 

</shape> 

đây nếu bạn thiết lập các góc để các 270 màu sắc bắt đầu sẽ xuất hiện ở phía dưới và màu cuối ở đầu nếu bạn thiết lập các góc để 90 nó sẽ bị đảo ngược

0

Để hiển thị hiệu ứng tô chuyển tiếp từ trên xuống dưới trong android với độ trong suốt:

  1. Tạo tệp XML mới trong thư mục res/drawable có tên là gradient_transparent.xml và thêm mã sau đây.

.

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
     <gradient 
      android:startColor="#90000000" 
      android:endColor="#00ffffff" 
      android:angle="90" 
      /> 
    </shape> 

Trong file Layout XML

<LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@drawable/gradient_transparent" 
        android:orientation="vertical" 
        android:padding="@dimen/spacing_middle"> 
</LinearLayout> 
Các vấn đề liên quan