Android設置背景色可以通過在res/drawable裏定義一個xml,如下:
[代碼]xml代碼:
1 |
<? xml version = "1.0" encoding = "utf-8" ?> |
2 |
< shape xmlns:android = "http://schemas.android.com/apk/res/android" > |
4 |
android:startColor = "#FFF" |
5 |
android:endColor = "#000" |
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" > |
4 |
android:startColor = "#FFF" |
5 |
android:endColor = "#000" |
第二步:
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" > |
第三步:
[代碼]java代碼:
01 |
import android.app.Activity; |
02 |
import android.os.Bundle; |
04 |
public class LoginActivity extends Activity
{ |
06 |
public void onCreate(Bundle
savedInstanceState) { |
07 |
super .onCreate(savedInstanceState); |
08 |
setContentView(R.layout.login); |
效果圖: