reactjs - 如何在 React Native 中让父级的宽度包裹其内容?

标签 reactjs react-native

我是 React Native 的新手,所以这可能是一个愚蠢的问题。

我想要得到的是这样的: enter image description here

我有一个带有 2 个 subview 的父 View ,即图像和文本,垂直向左对齐。我想要的是父 View 仅覆盖其 subview 的宽度。但我得到的是这样的:

enter image description here

父 View 拉伸(stretch)到移动屏幕的整个宽度。

这是我的代码:

render () (<View style={{backgroundColor: '#333333'}}>
  <Image source={btnImageSource} />
  <Text>Some label</Text>
</View>);

类似于 Android 的“wrap-content”宽度值。

最佳答案

您将能够使用flex来做到这一点。我找到了这个guide在使用 flex 时非常有帮助。

一个好的起点是创建一个父 View ,即 flexDirection: row, justifyContent: flex-start

render () (
    <View style={{justifyContent: 'flex-start', flexDirection: 'row'}}>
        <View style={{backgroundColor: '#333333'}}>
            <Image source={btnImageSource} />
            <Text>Some label</Text>
        </View>
    </View>
);

关于reactjs - 如何在 React Native 中让父级的宽度包裹其内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951359/

相关文章:

javascript - mapStateToProps 应该异步更新 props 吗?

javascript - 在事件中启用 react 渲染或禁用它

javascript - 类型错误 : Cannot read property 'firstName' of undefined - using 'props' in react

javascript - 使用 ES5 的固定数据表示例

react-native - 如何嵌套createDrawerNavigator createStackNavigator createBottomTabNavigator

javascript - 将 Prop 从 Modal 传递到其他组件 react 导航

ios - 在Ios中 react 原生Google登录问题

reactjs - 使用react-pdf渲染具有自定义高度和宽度的pdf时无法隐藏iframe工具栏菜单

javascript - 'react' 导出的函数

ios - 在 React Native 0.68 中启用 TurboModule 和 Fabric 会导致很多错误