앱 스타일 지정은 다음과 같이 수행할 수 있습니다. −
- 스타일시트 구성요소 사용
- 인라인 스타일 사용
스타일시트 구성요소 사용
React 기본 Stylesheet 구성 요소는 앱에 스타일을 적용하고 싶을 때 매우 편리하고 깔끔합니다. Stylesheet 구성 요소로 작업하려면 먼저 아래와 같이 가져옵니다. -
import { StyleSheet } from 'react-native'; 다음과 같이 Stylesheet 컴포넌트를 사용하여 스타일을 생성할 수 있습니다 -
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
},
item: {
margin: 10,
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
}
}); 위의 스타일은 코드 내에서 다음과 같이 사용할 수 있습니다 -
<View style={styles.container}></View> 다음은 FlatList 구성 요소의 표시를 위해 Stylesheet를 사용하는 예입니다 -
예시 1
import React from "react";
import { FlatList , Text, View, StyleSheet, StatusBar } from "react-native";
export default class App extends React.Component {
constructor() {
super();
this.state = {
data: [
{ name: "Javascript Frameworks", isTitle: true },
{ name: "Angular", isTitle: false },
{ name: "ReactJS", isTitle: false },
{ name: "VueJS", isTitle: false },
{ name: "ReactNative", isTitle: false },
{ name: "PHP Frameworks", isTitle: true },
{ name: "Laravel", isTitle: false },
{ name: "CodeIgniter", isTitle: false },
{ name: "CakePHP", isTitle: false },
{ name: "Symfony", isTitle: false }
],
stickyHeaderIndices: []
};
}
renderItem = ({ item }) => {
return (
<View style={styles.item}>
<Text style={{ fontWeight: (item.isTitle) ? "bold" : "", color: (item.isTitle) ? "red" : "gray"}} >
{item.name}
</Text>
</View>
);
};
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.data}
renderItem={this.renderItem}
keyExtractor={item => item.name}
stickyHeaderIndices={this.state.stickyHeaderIndices}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
},
item: {
margin: 10,
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
}
}); 출력

인라인 스타일 사용
스타일 속성을 사용하여 스타일을 인라인으로 추가할 수 있습니다. 그러나 이것은 코드를 읽기 어려울 수 있기 때문에 모범 사례가 아닙니다. 다음은 반응형 구성 요소 내에서 인라인 스타일을 사용하는 방법에 대한 실제 예입니다.
예시 2
기본 앱 내보내기
import React from 'react';
import { Button, View, Alert } from 'react-native';
const App = () => {
return (
<View style={{flex :1, justifyContent: 'center', margin: 15 }}>
<Button
title="Click Me"
color="#9C27B0"
onPress={() => Alert.alert('Testing Button for React Native ')}
/>
</View>
);
} 출력
