官术网_书友最值得收藏!

Touchable opacity

When a button needs a custom look, it quickly seems like you need a better alternative. This is where TouchableOpacity comes into play. It serves every purpose when inner content needs to become touchable. Hence, we will make our own button and style it as we like:

class LikeCounter extends React.Component {
state = {
likeCount: 0
}
like = () => this.setState({likeCount: this.state.likeCount + 1})
unlike = () => this.setState({likeCount: this.state.likeCount - 1})

render = () => (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}

onPress={this.unlike}
>
<Text>Unlike</Text>
</TouchableOpacity>
<Text style={styles.text}>{this.state.likeCount}</Text>
<TouchableOpacity
style={styles.button}

onPress={this.like}
>
<Text>Like</Text>
</TouchableOpacity>
</View>
);
}

Some example styles follow. We will dig further into styles in Chapter 3, Styling Patterns:

const styles = StyleSheet.create({
container: {
flexDirection: 'row',
paddingTop: 20,
paddingLeft: 20
},
button: {
alignItems: 'center', // horizontally centered
justifyContent: 'center', // vertically centered
backgroundColor: '#DDDDDD',
padding: 20
}
,
text: {
fontSize: 45
}
});

The button's contents are centered vertically and horizontally. We have a custom gray background color and padding inside of the button. Padding is the space from the children to the border of the component.

Now that we know about these simple components, we are ready to proceed further and explore how forms are built and how to handle more complicated use cases.

主站蜘蛛池模板: 牙克石市| 永泰县| 临猗县| 平湖市| 石狮市| 平和县| 林西县| 织金县| 白山市| 永济市| 蕉岭县| 金华市| 洮南市| 永春县| 全州县| 晴隆县| 苍南县| 淄博市| 海门市| 平定县| 河间市| 新田县| 库车县| 库尔勒市| 皋兰县| 凯里市| 萝北县| 榕江县| 修水县| 麻栗坡县| 咸丰县| 靖宇县| 正阳县| 青阳县| 泽普县| 临安市| 邵阳市| 乐亭县| 贡嘎县| 百色市| 嘉善县|