I want to make a ListView and each row should contain an Icon and a Text. But I need them to be vertically aligned.
The code:
JavaScript
x
40
40
1
export default class SettingsPage extends Component {
2
3
constructor() {
4
super();
5
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
6
this.state = {
7
dataSource: ds.cloneWithRows([
8
{
9
iconName: 'bell',
10
title: 'bell'
11
},
12
{
13
iconName: 'map-marker',
14
title: 'map'
15
},
16
]),
17
};
18
}
19
20
_renderRow(rowData) {
21
return (
22
<View style={{borderBottomWidth:1, padding:20}}>
23
<Text>
24
<Icon name={rowData.iconName} size={40}/>
25
<Text style={{fontSize:25}}> {rowData.title}</Text>
26
</Text>
27
</View>
28
);
29
}
30
31
render() {
32
return (
33
<ListView
34
dataSource={this.state.dataSource}
35
renderRow={this._renderRow}
36
/>
37
);
38
}
39
}
40
The above code generates:
in which the components are not aligned.
How can I solve this?
Advertisement
Answer
Try something like this:
JavaScript
1
13
13
1
_renderRow(rowData) {
2
return (
3
<View style={{borderBottomWidth:1, padding:20, flex: 1, flexDirection: 'row'}}>
4
<View style={{flex: 1}}>
5
<Icon name={rowData.iconName} size={40}/>
6
</View>
7
<View style={{flex: 5}}>
8
<Text style={{fontSize:25}}> {rowData.title}</Text>
9
</View>
10
</View>
11
);
12
}
13
Tweak the flex value of the <View>
wrapping the text element for a result more to your liking.