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

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.

主站蜘蛛池模板: 电白县| 花莲县| 马龙县| 册亨县| 沁阳市| 江口县| 鸡东县| 广昌县| 乐业县| 洛南县| 廊坊市| 阳春市| 治县。| 萨迦县| 乌恰县| 科技| 东阳市| 抚州市| 闽清县| 昌图县| 文安县| 吴江市| 三门县| 容城县| 杭锦后旗| 宁远县| 常德市| 祁连县| 田林县| 南通市| 惠水县| 惠水县| 屏南县| 阿坝| 张掖市| 隆德县| 岳阳县| 花莲县| 云梦县| 台北县| 星子县|