Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android React Native 自定義組件的完整流程

編輯:關於 Android 程式設計

假設現在有這麼一個需求:自定義一個組件,由一個小圖標和圖標下的文字說明組成,並帶有可設置的背景色、寬度和高度。下圖正是兩個這樣的組件拼成的效果。

第一步:建立組件文件

在 index.android.js 所在目錄新建一個 item.js,寫入模板代碼:

'use strict';
var React = require('react-native');
var { StyleSheet, Text, View, Image, } = React;

var Item = React.createClass({
    render: function() {
        return (
            <View>
            </View>
        );
    },
});

var styles = StyleSheet.create({});

module.exports = Item;

注意最後一句 module.exports = Item;,它負責把組件導出。

第二步:組織結構與樣式

組件最外圍是一個 View,內部嵌套一個 Image 和一個 Text。圖片地址與文本內容需要可設置,而 View 的背景色、寬高這些樣式應由外部傳入,並與組件內部樣式聯合作用。這一點可以通過樣式數組實現,即 style={[style1, style2]}。render 返回的結構如下:

<View style={[styles.item, outStyle]}>
    <Image style={styles.image} source={{uri: img}} />
    <Text style={styles.text}>{text}</Text>
</View>

再通過 this.props 拿到外部傳入的屬性值:

var outStyle = {
    width: parseInt(this.props.width),
    height: parseInt(this.props.height),
    backgroundColor: this.props.color,
}
var img = this.props.img;
var text = this.props.text;

完整的 render 函數:

var Item = React.createClass({
    render: function() {
        var outStyle = {
            width: parseInt(this.props.width),
            height: parseInt(this.props.height),
            backgroundColor: this.props.color,
        }
        var img = this.props.img;
        var text = this.props.text;
        return (
            <View style={[styles.item, outStyle]}>
                <Image style={styles.image} source={{uri: img}} />
                <Text style={styles.text}>{text}</Text>
            </View>
        );
    },
});

內部樣式定義如下:

var styles = StyleSheet.create({
    item: {
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 10,
        marginLeft: 5,
        marginRight: 5,
        marginTop: 5,
        marginBottom: 5,
    },
    image: {
        width: 48,
        height: 48,
    },
    text: {
        color: '#ffffff',
        alignItems: 'center',
    },
});

這個 View 其實還能提取更多屬性作為自定義屬性,這裡僅演示幾個。

如何使用

使用時,先在文件中引入組件,再用自定義標簽並設置屬性即可。

引入組件:

var Item = require('./item');

使用組件:

var AwesomeProject = React.createClass({
    render: function() {
        return (
            <View style={styles.container}>
                <Item style={styles.item1} color='#aaa' width='100' height='100'
                    text='Icon'
                    img='https://raw.githubusercontent.com/lizhangqu/androidicons/master/assets/blue_dark/xhdpi/ic_action_achievement.png'></Item>
                <Item style={styles.item2} color='#aaa' width='100' height='100'
                    text='Icon'
                    img='https://raw.githubusercontent.com/lizhangqu/androidicons/master/assets/blue_dark/xhdpi/ic_action_bike.png'></Item>
            </View>
        );
    },
});

var styles = StyleSheet.create({
    container: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#ff0000',
    },
    item1: { marginLeft: 100, },
    item2: { marginLeft: 100, },
});

可以看到,我們在 render 裡使用了 Item 標簽,並為它設置了自定義屬性,這些屬性最終會被應用到組件上。至此,一個可復用的自定義組件就完成了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved