Android背景渐变色(shape,gradient)

本文介绍如何在Android中使用XML定义背景渐变色。通过创建一个包含渐变属性的shape元素,可以轻松地为应用界面添加从一种颜色平滑过渡到另一种颜色的效果。文章详细展示了如何设置起始颜色、结束颜色及渐变角度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://l62s.iteye.com/blog/1659433

Android设置背景色可以通过在res/drawable里定义一个xml,如下:

[代码]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代码:

<?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代码:

<?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>
第三步:

[代码]java代码:

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);
	    }
}

效果图:

渐变颜色效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值