![](/img/trans.png)
[英]Is there a way I can wrap the whole contents of a Text component inside parent view in react-native?
[英]Is there a way to wrap background color around text in react-native?
視圖中的本機默認為拉伸的alignItems
道具。 但這意味着什么?
考慮到邊距,您視圖的所有子項都將拉伸以填充其父項的寬度。
如果您將父視圖的alignItems
屬性設置為stretch
其他內容,例如center
、 flex-start
或flex-end
您將看到背景顏色僅圍繞您的實際文本延伸。
您還可以在 Text 視圖上使用alignSelf
來調整單個項目。
這是一個示例小吃https://snack.expo.io/@ajthyng/vengeful-celery
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
const BGText = props => {
const { background } = props;
return <Text style={{backgroundColor: background, ...props.style}}>{props.children}</Text>;
}
export default class App extends React.Component {
render() {
return (
<View style={{flex: 1, backgroundColor: 'white', alignItems: 'stretch', marginTop: 23}}>
<BGText background='pink' style={{marginRight: 16}}>I am some text</BGText>
<BGText background='orange' style={{alignSelf: 'flex-start', marginLeft: 16}} >My BG Color is short</BGText>
</View>
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.