首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Native:如何扩展TouchableOpacity

React Native:如何扩展TouchableOpacity
EN

Stack Overflow用户
提问于 2021-03-16 04:28:10
回答 2查看 67关注 0票数 0

我正在尝试让两个大按钮(TouchableOpacity)相邻并占据屏幕宽度的50%。

但它们似乎只与其中的文本组件一样宽。

怎样才能让两个TouchableOpacity占据整个屏幕的宽度,每个占据50%的宽度?

代码语言:javascript
复制
  const styles = StyleSheet.create({
    container: {
      flexDirection: 'row',
      backgroundColor: 'lightblue',
      height: 200,
      justifyContent: 'space-around',
    },
    btn: {
      flex: 1,
      backgroundColor: 'red',
      justifyContent: 'center',
      padding: 20,
      borderWidth: 1,
    },
  });

  return (
    <View style={styles.container}>
      <TouchableOpacity style={styles.btn}>
        <Text>Left</Text>
      </TouchableOpacity>

      <TouchableOpacity style={styles.btn}>
        <Text>Right</Text>
      </TouchableOpacity>
    </View>
  );
EN

回答 2

Stack Overflow用户

发布于 2021-03-16 05:02:48

尝试为您的btn类添加50%的宽度

代码语言:javascript
复制
  const styles = StyleSheet.create({
    container: {
      flexDirection: 'row',
      backgroundColor: 'lightblue',
      height: 200,
      justifyContent: 'space-around',
    },
    btn: {
      width: '50%',
      backgroundColor: 'red',
      padding: 20,
      borderWidth: 1,
    },
  });

您也可以从react原生使用dimensions,并将windowWidth:/2放在您的btn类上。

票数 1
EN

Stack Overflow用户

发布于 2021-03-16 05:19:17

一种可能的解决方案是使用react原生的Dimensions

https://reactnative.dev/docs/dimensions

代码语言:javascript
复制
  import { Dimensions } from 'react-native';

  const width = Dimensions.get('window').width;

  const styles = StyleSheet.create({
    container: {
      flexDirection: 'row',
      backgroundColor: 'lightblue',
      height: 200,
      justifyContent: 'space-around',
    },
    btn: {
      width: width*0.5,
      backgroundColor: 'red',
      padding: 20,
      borderWidth: 1,
    },   
  });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66645207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档