gpt4 book ai didi

ios - react native : how to put element to the right in parent view

转载 作者:可可西里 更新时间:2023-11-01 06:24:11 25 4
gpt4 key购买 nike

我想知道如何让红色标签向右移动?

enter image description here

似乎,flex 不支持任何已知的东西,如“align:right”或“float:right”。

我试过 flex: 1 和 flex: 2,最后删除了 flex,这没什么区别。

如何将一个元素放在右边,而其他元素留在左边?

renderItem(item) {

return (
<TouchableHighlight onPress={() => this.showItemDetail(item)} underlayColor='#dddddd'>
<View>
<View style={styles.container}>
<View style={styles.itemRow}>
<Text style={styles.title}>{item.name}</Text>
<View style={styles.detailBox}>
<View>
<Text style={styles.details}>opened: {item.getOpenedFromNow()}</Text>
<Text style={styles.details}>closed: {item.getClosedFromNow()}</Text>
</View>
<View>
{this.getClosingInfo(item)}
</View>
</View>
</View>
</View>
<View style={styles.separator}/>
</View>
</TouchableHighlight>
);
}

样式(摘录):

container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fffce1',
padding: 15,
},

detailBox: {
flex: 1,
},
itemRow: {
flex: 1,
marginTop: -10
},
title: {
fontSize: 15,
marginBottom: 8
},
closed: {
fontSize: 15,
color: 'red',
},

最佳答案

您可以将 detailBox 项目包装在一个 flexbox 容器中,使用 flex-direction:row,然后将 flex:1 设置为两个主要子组件:

<View style={styles.detailBox}>
<View style={styles.box1}>
<Text style={styles.details}>opened: {item.opened}</Text>
<Text style={styles.details}>closed: {item.closed}</Text>
</View>
<View style={styles.box2}>
<Text style={styles.status}>{item.status}</Text>
</View>
</View>

样式:

box1: {
flex:1,
},
box2: {
flex:1,
},
detailBox: {
flexDirection: 'row'
}

我还设置了一个工作示例 here (下面的代码)。

https://rnplay.org/apps/_yOSHw

'use strict';

var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
TouchableHighlight
} = React;

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
var data = [{name: 'issue 1', opened: '2 days ago', closed: 'a day ago', status: 'CLOSED'}, {name: 'issue 2', opened: '10 days ago', closed: '3 days ago', status: 'CLOSED'}]

var SampleApp = React.createClass({

getInitialState() {
return {
dataSource: ds.cloneWithRows(data),
};
},

renderItems(item) {
return (
<TouchableHighlight onPress={() => this.showItemDetail(item)} underlayColor='#dddddd' style={{ backgroundColor: '#fffce2', borderBottomWidth:1, borderBottomColor: '#ededed' }}>
<View>
<View>
<View style={styles.itemRow}>
<Text style={styles.title}>{item.name}</Text>
<View style={styles.detailBox}>
<View style={styles.box1}>
<Text style={styles.details}>opened: {item.opened}</Text>
<Text style={styles.details}>closed: {item.closed}</Text>
</View>
<View style={styles.box2}>
<Text style={styles.status}>{item.status}</Text>
</View>
</View>
</View>
</View>
<View style={styles.separator}/>
</View>
</TouchableHighlight>
);
},

render: function() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderItems}
/>
</View>
);
}
});

var styles = StyleSheet.create({
container: {
flex: 1,
marginTop:60,
},
status: {
textAlign: 'center',
paddingRight:20,
color: 'red',
fontSize:18
},
box1: {
flex:1,
padding:10
},
box2: {
flex:1,
padding:10
},
title: {
fontSize:18,
paddingTop:10,
paddingLeft:10
},
detailBox: {
flexDirection: 'row'
},
separator: {

}
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

关于ios - react native : how to put element to the right in parent view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34615573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com