2015-11-10 103 views
1

我正在尋找在我的行右側設置日期的位置。無法將文本對齊到該行的右側

下面是代碼:

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    marginTop: 64, 
    }, 
    row: { 
    flexDirection: 'row', 
    alignItems: 'center', 
    margin: 5, 
    backgroundColor: 'green', 
    }, 
    horizontalAlign: { 
    flexDirection: 'row', 
    alignItems: 'center', 
    backgroundColor: 'red', 
    }, 
    rowTitle: { 
    fontFamily: 'Helvetica', 
    fontSize: 17, 
    marginLeft: 10, 
    flex: 1, 
    textAlign:'center' 
    }, 
    rowDate: { 
    textAlign: 'right', 
    }, 
    rowText: { 
    fontFamily: 'Helvetica', 
    fontSize: 17, 
    marginLeft: 10, 
    }, 
    rowImage: { 
    width: 35, 
    height: 35, 
    borderRadius: 17.5, 
    }, 
}); 

renderRow(rowData, sectionID, rowID, _highlightRow) { 
    return (
    <TouchableHighlight onPress={() => { this.rowPressed(rowData.recordID || null); }}> 
     <View style={styles.row}> 
     <Image source={ require('myImage.png') } style={styles.rowImage} /> 
     <View> 
      <View style={styles.horizontalAlign}> 
      <Text style={styles.rowTitle}>{rowData.title}</Text> 
      <Text style={styles.rowDate}>{rowData.date}</Text> 
      </View> 
      <Text style={styles.rowText}>{rowData.text}</Text> 
     </View> 
     </View> 
    </TouchableHighlight> 
); 
} 

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

截圖:

enter image description here

什麼建議嗎?

+0

你是不是想將日期對準了整個屏幕的右側?謝謝。 –

+0

與'View'右側對齊(使用'style = {styles.row}') –

回答

2

您需要添加的柔性:1個屬性到包含rowData.title視圖和rowData.date。我已經用你的數據here設置了一個例子。

<View style={{flex:1}}> 
    <View style={styles.horizontalAlign}> 
    <Text style={styles.rowTitle}>{rowData.title}</Text> 
    <Text style={styles.rowDate}>{rowData.date}</Text> 
    </View> 
    <Text style={styles.rowText}>{rowData.text}</Text> 
</View> 

https://rnplay.org/apps/mWO0LQ

1

我對React不熟悉,但在標準CSS中,將margin-left: auto應用於日期項目。

您還可以將justify-content: space-between應用於柔性容器,該柔性容器將在相對的邊緣上對齊兩個柔性物品。

(這些解決方案假定在其上的柔性物品存在是自由的線延伸到所述容器的整個寬度。)

+0

感謝您的回答,但不幸的是沒有任何提案完成了這項工作。但是,我注意到'alignSelf:'flex-end''將標題的日期與標題垂直對齊,而不是水平對齊。 沒有alignSelf:http://img11.hostingpics.net/pics/747396ScreenShot20151110at155847.png 隨着alignSelf:http://img11.hostingpics.net/pics/567878ScreenShot20151110at155858.png –

+0

嘗試'柔性成長:1'或在'View style = {styles.row}'(或'horizo​​ntalAlign')上使用'flex-basis:100%'或'width:100%',然後試試我的答案。 –

+1

感謝您的幫助,但@Naber的回答解決了我的問題。 –

相關問題