简体   繁体   English

React Native:如何使用 textInput 内联切换开关?

[英]React Native: How to inline a toggle switch with a textInput?

This is what I have done so far:这是我到目前为止所做的:

在此处输入图像描述

I want the text input to extent until reaches end of box.我希望文本输入一直延伸到框的末尾。

Here's my code:这是我的代码:

<View style={{ flexDirection: 'row' }} >
     <Switch style={styles.switch}
             trackColor={{true: "#36d79a", false: 'grey'}}
             thumbColor={{true: "#36d79a", false: 'grey'}}
     />
    <Input right placeholder="Type your custom question here." iconContent= 
   {<Block />} 
     />
</View>

To fill space along the main axis of flexboxes, apply the flex property with a number value to flex children.要沿着 flexboxes 的主轴填充空间,请将具有数字值的flex属性应用于 flex 子项。 The number specifies the proportions of how the available space is distributed among the flex children.该数字指定了可用空间在 flex 子节点之间的分配比例。 See the docs on flexbox for details.有关详细信息,请参阅flexbox上的文档。

In your case, you would specify flex: 1 only for the <Input /> meaning that this component alone is allowed to fill the rest of the space.在您的情况下,您将仅为<Input />指定flex: 1 ,这意味着仅允许此组件填充空间的 rest 。 I've created this React Native & MUI Codesandbox to demonstrate it.我创建了这个React Native & MUI Codesandbox来演示它。

<View style={{ flexDirection: 'row' }} >
     <Switch style={styles.switch}
             trackColor={{true: "#36d79a", false: 'grey'}}
             thumbColor={{true: "#36d79a", false: 'grey'}}
     />
    <Input style={{ flex: 1 }} right placeholder="Type your custom question here." iconContent= 
   {<Block />} 
     />
</View>

Add justifyContent: 'space-between' to your main View.添加justifyContent: 'space-between'到你的主视图。 Alternatively, you can always use placeholder views: <View style={{ flex: 1 }} .或者,您始终可以使用占位符视图: <View style={{ flex: 1 }} You would put that placeholder between the Switch and the Input.您可以将该占位符放在 Switch 和 Input 之间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM