我想做 width: 100% - 50
所以我可以在它的右侧添加一个50宽的图标。
我有 width: 100% - 20%
使用 react-native-extended-styles 工作,但我不明白为什么这很有用,因为你可以做 width: '80%'
。我无法 width: 100% - 50
工作。有办法吗?
尝试使用 onLayout
事件来获取容器宽度,然后将容器宽度的 <autocomplete>
设置为 100% - 50
但它不起作用。
let Location = (props) => {
let locationInputElement
const blur = () => {
locationInputElement.blur()
}
let inputContainerWidth
return (
<View style={styles.formItem}>
<View
onLayout={(event) => {
inputContainerWidth = event.nativeEvent.layout.width
console.log(inputContainerWidth)
}}
<Autocomplete
data={props.autocompleteResults.predictions}...
style={{
borderRadius: 8,
backgroundColor: 'red',
alignSelf: 'stretch',
paddingLeft: 10,
position: 'relative',
...styles.label,
...styles.labelHeight,
width: inputContainerWidth - 50
}}
/>
</View>
</View>
)
}
它确实 console.log 335
当它 console.logs inputContainerWidth
但 <autocomplete>
的宽度仍然是 100%。
原文由 BeniaminoBaggins 发布,翻译遵循 CC BY-SA 4.0 许可协议
我同意 Viktor 的观点,你应该可以使用 Flex Box 来实现这一点。
这是我放在一起的东西: https ://snack.expo.io/B1jDKOhyb
You set the
flexDirection
of the formRow torow
, and then the first child (the holderView
for your AutoComplete component toflex: 1
.这使它填满了所有可用空间。下一个孩子View
是您的图标持有者。您可以将其设置为您想要的任何值(在本例中为50)。