我想在React Native中将一个视图放在另一个视图的中心.

这是我的代码:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'yellow',
  },
  outerCircle: {
    backgroundColor: 'blue',
    width: 100,
    height: 100,
    borderRadius: 100/2,
  },
  innerCircle: {
    backgroundColor: 'red',
    width: 80,
    height: 80,
    borderRadius: 80/2,
  }
});

export default class RecorderButton extends React.Component {

  _buttonPressAction() {
    Alert.alert("button pressed");
  }

  render() {
    return (
      <TouchableOpacity activeOpacity={0.4}
                        onPress={this._buttonPressAction}
                        style={styles.container}>
        <View style={styles.outerCircle}>
          <View style={styles.innerCircle} />
        </View>
      </TouchableOpacity>
    );
  }
}

这就是它的样子:

我想要蓝色和红色的同心圆.我如何做到这一点?

推荐答案

你已经在容器中居中了.对于outerCircle也要遵循同样的方法.

  outerCircle: {
    backgroundColor: 'blue',
    width: 100,
    height: 100,
    borderRadius: 100/2,
    justifyContent: 'center',
    alignItems: 'center'
  },

React-native相关问答推荐

嵌套的FlatList内的Incorrect Padding

使用动画标记时如何优化react 本机 map 性能

如何在 react-navigation v6 中更改标题高度

需要以前的props和state回调的静态 getDerivedStateFromProps?

react-redux connect 和 redux 数据的组件生命周期顺序

如何在 React Native 的嵌套 Touchable 中传播touch 事件?

为应用创建单个 MobX store 的最佳方式是什么?

React-native,iOS模拟器停止响应CMD + D

React Native 无法读取 index.android.delta

React Native:无法点击调试菜单

为什么 Android Studio 强制使用 Android 支持库中的 Androidx?

react-native 自动完成文本输入

Rich ReactNative TextInput

如何使用 Expo 在真实的 iOS 设备上运行应用程序?

TypeError : props.navigation.getParam 不是函数. In(props.navigation.getParam('name')

将 React Native 升级到 0.60-RC2 后找不到库libjsc.so

区别需要MainQueueSetup 和dispatch_get_main_queue?

INSTALL_FAILED_INSUFFICIENT_STORAGE 运行 npm run android 命令时出错

如何在 react-native 中设置 Alert 元素的样式?

React Native:无法解析模块 fs