css - 使用显式高度时 react native 垂直文本对齐

标签 css react-native flexbox react-native-flexbox

我想将文本在具有明确高度的 View 内垂直居中。

Center text vertically in react-native没有完全回答我的问题,因为他们没有使用明确的高度。

当我使用时

  <View style={styles.rightContainer}>

            <Text style={styles.label2}>
              Centered
            </Text>

        </View>
  rightContainer: {
    marginRight: 10,
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
   label2: {
    height: 40,
    backgroundColor: 'green',
    textAlignVertical: 'center',
  },

result1

如果我去掉明确的高度,

  label2: {
    backgroundColor: 'green',
    textAlignVertical: 'center',
  },

it works

它有效。

我需要设置明确的高度,因为我会将此 View 转换为可点击的按钮。

我该如何进行这项工作? 我对 react 原生布局有什么误解?

最佳答案

这是因为“textAlignVertical”属性仅适用于 Android。检查documentation在文本上。

您可以添加一个额外的容器来包裹文本:

export default class FlexDirectionBasics extends Component {
  render() {
    return (
      <View style={styles.rightContainer}>
        <View style={styles.labelContainer}>
          <Text style={styles.label}>Centered</Text>
        </View>
      </View>
    );
  }
}

const styles = {
  rightContainer: {
    marginRight: 10,
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  labelContainer: {
    height: 40,
    backgroundColor: 'green',
    alignItems: 'center',
    justifyContent: 'center',
  },
  label2: {},
};

关于css - 使用显式高度时 react native 垂直文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57641742/

相关文章:

css - Susy Omega 无法正确设置列宽

php - while循环导致网站消失

javascript - 在 React 中将 onChange 事件从子级传递给父级

android - React Native Geolocation Watchposition 不会实时更新

html - CSS clear left every 3rd element

html - 使用 flex 使容器全宽

html - 如何正确放置旋转文本?

jquery 动画所有 child 的字体大小

javascript - 如何在 React Native 中绘制图片?

css - 居中主div