Android背景漸變色(shape,gradient)

Android設置背景色可以通過在res/drawable裏定義一個xml,如下:

[代碼]xml代碼:

1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android">
3     <gradient
4         android:startColor="#FFF"
5         android:endColor="#000"
6         android:angle="45" />
7 </shape>

shape是用來定義形狀的,gradient定義該形狀裏面爲漸變色填充,startColor起始顏色,endColor結束顏色,angle表示方向角度。當angle=0時,漸變色是從左向右。 然後逆時針方向轉,當angle=90時爲從下往上。

實現過程

第一步:

res/drawable/background_login.xml

[代碼]xml代碼:

1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android">
3     <gradient
4         android:startColor="#FFF"
5         android:endColor="#000"
6         android:angle="45" />
7 </shape>

第二步:

res/layout/login.xml

[代碼]xml代碼:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     android:orientation="vertical"
4     android:layout_width="fill_parent"
5     android:layout_height="fill_parent"
6     android:background="@drawable/background_login">
7 </LinearLayout>
第三步:

[代碼]java代碼:

01 import android.app.Activity;
02 import android.os.Bundle;
03   
04 public class LoginActivity extends Activity {
05     @Override
06     public void onCreate(Bundle savedInstanceState) {
07         super.onCreate(savedInstanceState);
08         setContentView(R.layout.login);
09     }
10 }

效果圖:

漸變顏色效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章