首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Native stretch行项目?

React Native stretch行项目?
EN

Stack Overflow用户
提问于 2018-06-14 16:39:01
回答 4查看 5.4K关注 0票数 5

由于某种原因,我不得不在视图中一个接一个地按下按钮,即使我将视图设置为alignItems:'stretch‘或将项目设置为alignSelf:'stretch’,它们也不会用完可用的空间。我该怎么解决这个问题呢?

例如:

代码语言:javascript
复制
<View style={{flexDirection: 'row', alignItems: 'stretch'}}>
    <View style={{backgroundColor: 'red', height: 100}}/>
    <View style={{backgroundColor: 'blue', height: 100}}/>
</View>

视图不会拉伸,内部元素将保持宽度:0

或与按钮项相同:

代码语言:javascript
复制
<View style={{flexDirection: 'row', alignItems: 'stretch'}}>
    <Button title='text' style={{backgroundColor: 'red', 
    height: 100}}/>
    <Button title='text' style={{backgroundColor: 'blue', 
    height: 100}}/>
</View>
EN

回答 4

Stack Overflow用户

发布于 2018-06-14 17:01:55

您需要为主视图提供width。所以你可以根据它来设置按钮。此外,您还需要为每个按钮设置flex

代码语言:javascript
复制
<View style={{flexDirection: 'row', width:'100%', flex:1}}>
    <Button title='text' style={{backgroundColor: 'red', 
    height: 100, flex:1}}/>
    <Button title='text' style={{backgroundColor: 'blue', 
    height: 100, flex:1}}/>
</View>
票数 4
EN

Stack Overflow用户

发布于 2018-06-14 17:44:38

我只是意识到你在使用alignItems,而你应该使用justifyContent在主轴上展开你的项目(在你的例子中是水平的)。拉伸不适用于justifyContent,因此您可以选择间隔或均匀间隔,例如:

代码语言:javascript
复制
<View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
    <View style={{backgroundColor: 'red', height: 100}}/>
    <View style={{backgroundColor: 'blue', height: 100}}/>
</View>
票数 1
EN

Stack Overflow用户

发布于 2021-02-26 20:26:51

您必须将按钮包装在另一个View组件中:

代码语言:javascript
复制
<View style={{flexDirection: "row"}}>
    <View style = {{flex: 1}}>
        <Button title="Button 1" />
    </View>
    <View style = {{flex: 1}}>
       <Button title="Button 2" />
    </View>
</View>

或者与TouchableOpacity一起使用Text组件

代码语言:javascript
复制
<View style={{ flexDirection: "row"}}>
    <TouchableOpacity style = {{width: "50%"}}>
        <Text style={{textAlign:"center"}} > Button 1 </Text>
    </TouchableOpacity>
    <TouchableOpacity style = {{width: "50%"}}>
        <Text style={{textAlign:"center"}} > Button 1 </Text>
    </TouchableOpacity>
</View>

在这里尝试这两种解决方案:https://snack.expo.io/wAENhUQrp

按钮不会占用所有可用空间buttons

  • button {alignSelf: 'stretch'}在按钮组件

上不起作用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50853168

复制
相关文章

相似问题

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