javascript - 在 ListView 中垂直对齐图标和文本 - React Native
问题描述
我想做一个 ListView,每一行都应该包含一个图标和一个文本。但我需要它们垂直对齐。
编码:
export default class SettingsPage extends Component {
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows([
{
iconName: 'bell',
title: 'bell'
},
{
iconName: 'map-marker',
title: 'map'
},
]),
};
}
_renderRow(rowData) {
return (
<View style={{borderBottomWidth:1, padding:20}}>
<Text>
<Icon name={rowData.iconName} size={40}/>
<Text style={{fontSize:25}}> {rowData.title}</Text>
</Text>
</View>
);
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
);
}
}
上面的代码生成:
其中组件未对齐。
我该如何解决这个问题?
解决方案
尝试这样的事情:
_renderRow(rowData) {
return (
<View style={{borderBottomWidth:1, padding:20, flex: 1, flexDirection: 'row'}}>
<View style={{flex: 1}}>
<Icon name={rowData.iconName} size={40}/>
</View>
<View style={{flex: 5}}>
<Text style={{fontSize:25}}> {rowData.title}</Text>
</View>
</View>
);
}
调整<View>
包装文本元素的 flex 值,以获得更符合您喜好的结果。
推荐阅读
- python - 在损失函数中索引 tf 变量
- dependency-injection - 升级后使用 Autofac 在 NServiceBus 中注册依赖关系
- caching - Android Webview 的本地文件路径
- python - 形状计算器循环
- ios - 将 UIImageView 调整为 UILabel 的高度
- elasticsearch - 如何扩展弹性搜索日期范围直方图聚合查询?
- python - Python:在构建器外部实例化类时引发警告
- c++ - 将 bash 参数传递给 C++
- javascript - JS过滤数组与另一个数组的对象
- apache-kafka - Accessing Kafka clusters using DNS