React Native中的对齐按钮

时间:2019-03-03 10:37:17

标签: react-native react-native-android

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, FlatList, Image, Button } from 'react-native';

export default class ListItems extends Component {
constructor(props) {
    super(props);
}

render() {
    return (
        <FlatList
            data={this.props.text}
            renderItem={(info) => (
                <TouchableOpacity onPress={() =>   this.props.selected(info.item.key)}>
                    <View style={styles.listitems}>
                        <Image resizeMode="contain" source={info.item.image} style={styles.image} />
                        <Text>{info.item.value}</Text>
                        <View style={styles.button}>
                        <Button title="Confirm" color="blue" onPress={this.props.itemDelete}></Button>
                        </View>
                    </View>
                </TouchableOpacity>
            )}
        >
        </FlatList>
        );
     }
 }

const styles = StyleSheet.create({
listitems: {
    width: "100%",
    flex:1,
    marginTop: 5,
    backgroundColor: "lightgrey",
    padding: 10,
    flexDirection: 'row',
},
image: {
    marginRight: 8,
    height: 30,
    alignItems: "center",
    width: 40,
},
button: {
    width: "40%",
    alignItems: 'flex-end',
}
 })

我正在显示列表项,其中我的左侧图像较小,然后是文本,并且我希望我的按钮显示在最右侧,但是要使用alignItems:'flex-end'将其显示在中间。我该怎么办..我想在不使用positon的情况下进行此操作。I have added image of output

4 个答案:

答案 0 :(得分:0)

我认为那更好。

    renderItem={(info) => (
                <TouchableOpacity style={styles.itemWrapper} onPress={() =>   this.props.selected(info.item.key)}>
                    <View style={styles.itemImageContainer}>
                        <Image resizeMode="contain" source={info.item.image} style={styles.image} />
                        <Text>{info.item.value}</Text>
                    </View>
                    <View style={styles.button}>
                        <Button title="Confirm" color="blue" onPress={this.props.itemDelete}></Button>
                    </View>
                </TouchableOpacity>
            )}

const styles = StyleSheet.create({
itemWrapper: {
    flex:1,
    height: 70,
    marginTop: 5,
    backgroundColor: "lightgrey",
    padding: 10,
    flexDirection: 'row',
},
itemImageContainer: {
    flex: 2,
    alignItems: 'center',
},
image: {
    marginRight: 8,
    height: 30,
    width: 40,
},
buttonContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
}
 })

答案 1 :(得分:0)

您可以使用flex-endflex-startcenter对齐项目。 您必须在父组件中添加flex direction :'row'

buttonContainer: {
justifyContent: 'flex-end',}

答案 2 :(得分:0)

只需将justifyContent:'space-between',alignItems:'center添加到styles.listitems 这样可以解决您的问题。

答案 3 :(得分:0)

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, FlatList, Image, Button } from 'react-native';

export default class ListItems extends Component {
constructor(props) {
    super(props);
}

render() {
    return (
        <FlatList
            data={this.props.text}
            renderItem={(info) => (
                <TouchableOpacity onPress={() =>   this.props.selected(info.item.key)}>
                    <View style={styles.listitems}>
                      <View>
                        <Image resizeMode="contain" source={info.item.image} style={styles.image} />
                        <Text>{info.item.value}</Text>
                       <View>
                        <View style={styles.button}>
                        <Button title="Confirm" color="blue" onPress={this.props.itemDelete}></Button>
                        </View>
                    </View>
                </TouchableOpacity>
            )}
        >
        </FlatList>
        );
     }
 }

const styles = StyleSheet.create({
listitems: {
    width: "100%",
    flex:1,
    marginTop: 5,
    backgroundColor: "lightgrey",
    padding: 10,
    flexDirection: 'row',
    justifyContent:'space-between`enter code here`'
},
image: {
    marginRight: 8,
    height: 30,
    alignItems: "center",
    width: 40,
},
button: {
    width: "40%",
    alignItems: 'flex-end',
}
 })