css - React-native 使用 flexbox 定义组件高度

标签 css reactjs react-native

我试图定义一个具有相同高度的元素列表,但结果是该组件占用了所有可用空间。

这是代码:

    return (
      <View style={{flex: 1, flexDirection: 'row', justifyContent: "center"}} key={location.id}>
        <Text style={{width: "80%", height: 20}}>{location.name}</Text>
        <Button
          style={{width: "20%", height: 20}}
          title="x"
          onPress={() => this.removeLocationFromDevice(location)}
        />
      </View>
    )

这是结果:

This is the result

如何确保按钮和文本元素的高度刚好足以显示该元素,仅此而已?

最佳答案

您可以将初始 View 设置为 flex 1 并将按钮+文本包装在另一个 View 中

return (
  <View style={{flex: 1}} key='key'>
  <View style={{ flexDirection: 'row', justifyContent: "center"}}>
  <Text style={{width: "80%", height: 20}}>text</Text>
  <Button
    style={{width: "20%", height: 20}}
    title="x"
  />
  </View>
</View>
);

enter image description here

关于css - React-native 使用 flexbox 定义组件高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57073817/

相关文章:

jquery - 下拉菜单正在关闭,单击下拉区域的任意位置

css3 背景颜色不起作用

react-native - React-native-paper 上的 TextInput 不起作用

ios - 如果图像的外部边界 react native ,如何剪掉图像

javascript - 第二个参数 applicationverifier 必须是 firebase.auth.applicationverifier 的实现

javascript - 四个选择选项框根据每个选择框选项动态变化

css - 事件状态不适用于 CSS Sprite

javascript - 400(错误请求)并且找不到manifest.json

html - iOS 13.2 中 'Adding To Home Screen' 之后保留地址栏

reactjs - react-hook-form 在取消注册后保持验证字段