android 背景漸變色(shape,gradient)

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

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

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

 

實現過程:

第一步:

res/drawable/background_login.xml

 

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

第二步:

res/layout/login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/background_login">
 
</LinearLayout>

第三步:

import android.app.Activity;
import android.os.Bundle;

public class LoginActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
}


效果圖:



發佈了11 篇原創文章 · 獲贊 8 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章