This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo LinearGradient iconExpo LinearGradient

渲染渐变视图的通用 React 组件。

Android
iOS
tvOS
Web
Included in Expo Go
Recommended version:
~57.0.0

expo-linear-gradient 提供一个原生的 React 视图,可以在多个颜色之间以线性方向进行过渡。

信息 React Native 还在 View 组件上提供了 experimental_backgroundImage(Android 和 iOS)和 backgroundImage(Web)样式属性,这些属性支持 CSS 渐变语法,例如 linear-gradient()radial-gradient()。这可以作为不增加额外依赖的渐变背景的替代方案。由于这是一个实验性属性,如果遇到任何问题,请向 React Native 仓库报告

安装

🌐 Installation

Terminal
npx expo install expo-linear-gradient

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

Linear Gradient
import { StyleSheet, Text, View } from 'react-native'; import { LinearGradient } from 'expo-linear-gradient'; export default function App() { return ( <View style={styles.container}> <LinearGradient // Background Linear Gradient colors={['rgba(0,0,0,0.8)', 'transparent']} style={styles.background} /> <LinearGradient // Button Linear Gradient colors={['#4c669f', '#3b5998', '#192f6a']} style={styles.button}> <Text style={styles.text}>Sign in with Facebook</Text> </LinearGradient> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: 'orange', }, background: { position: 'absolute', left: 0, right: 0, top: 0, height: 300, }, button: { padding: 15, alignItems: 'center', borderRadius: 5, }, text: { backgroundColor: 'transparent', fontSize: 15, color: '#fff', }, });

应用接口

🌐 API

import { LinearGradient } from 'expo-linear-gradient';

Component

LinearGradient

Type: React.Component<LinearGradientProps>

Renders a native view that transitions between multiple colors in a linear direction.

LinearGradientProps

colors

Type: readonly [ColorValue, ColorValue, ...ColorValue[]]

A readonly array of colors that represent stops in the gradient. At least two colors are required (for a single-color background, use the style.backgroundColor prop on a View component).

For TypeScript to know the provided array has 2 or more values, it should be provided "inline" or typed as const.

dither

Only for:
Android

Optional • Type: boolean • Default: true

Enables or disables paint dithering. Dithering can reduce the gradient color banding issue. Setting false may improve gradient rendering performance.

end

Optional • Literal type: union

For example, { x: 0.1, y: 0.2 } means that the gradient will end 10% from the left and 20% from the bottom.

On web, this only changes the angle of the gradient because CSS gradients don't support changing the end position.

Acceptable values are: LinearGradientPoint | null

locations

Optional • Literal type: union • Default: []

A readonly array that contains numbers ranging from 0 to 1, inclusive, and is the same length as the colors property. Each number indicates a color-stop location where each respective color should be located. If not specified, the colors will be distributed evenly across the gradient.

For example, [0.5, 0.8] would render:

  • the first color, solid, from the beginning of the gradient view to 50% through (the middle);
  • a gradient from the first color to the second from the 50% point to the 80% point; and
  • the second color, solid, from the 80% point to the end of the gradient view.

The color-stop locations must be ascending from least to greatest.

Acceptable values are: readonly [number, number, ...number[]] | null

start

Optional • Literal type: union

For example, { x: 0.1, y: 0.2 } means that the gradient will start 10% from the left and 20% from the top.

On web, this only changes the angle of the gradient because CSS gradients don't support changing the starting position.

Acceptable values are: LinearGradientPoint | null

Inherited Props

Types

LinearGradientPoint

An object { x: number; y: number } or array [x, y] that represents the point at which the gradient starts or ends, as a fraction of the overall size of the gradient ranging from 0 to 1, inclusive.

Type: NativeLinearGradientPoint or object shaped as below:

PropertyTypeDescription
xnumber

A number ranging from 0 to 1, representing the position of gradient transformation.

ynumber

A number ranging from 0 to 1, representing the position of gradient transformation.

NativeLinearGradientPoint

Tuple: [x: number, y: number]