我想做width: 100% - 50,这样我就可以添加一个图标,它是50宽的右手边.

我用react-native-extended-styles得到了width: 100% - 20%,但我不明白为什么这是有用的,因为你可以做width: '80%'.我无法工作.有办法吗?

try 使用onLayout事件获取容器宽度,然后将容器宽度的<autocomplete>设置为100% - 50,但它不起作用.

    let Location = (props) => {
      let locationInputElement

      const blur = () => {
        locationInputElement.blur()
      }

      let inputContainerWidth

      return (
        <View style={styles.formItem}>
          <View
            onLayout={(event) => {
              inputContainerWidth = event.nativeEvent.layout.width
              console.log(inputContainerWidth)
            }}

    <Autocomplete
              data={props.autocompleteResults.predictions}...
 style={{
            borderRadius: 8,
            backgroundColor: 'red',
            alignSelf: 'stretch',
            paddingLeft: 10,
            position: 'relative',
            ...styles.label,
            ...styles.labelHeight,
            width: inputContainerWidth - 50
          }}
        />
      </View>
    </View>
  )
}

它确实是一种安慰.当它运行到控制台时记录335.原木inputContainerWidth,但<autocomplete>的宽度仍然是335%.

推荐答案

我同意Viktor的观点,你应该可以用Flex Box实现这一点.

这是我总结出来的:https://snack.expo.io/B1jDKOhyb

您将formRow的flexDirection设置为row,然后将第一个子项(AutoComplete组件的保持架View设置为flex: 1.这将填充所有可用空间.下一个子项View是图标保持架.您可以将其设置为任意值(在本例中为50).

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.formRow}>
          <View style={styles.formItem}>
            // AutoComplete component goes here
          </View>
          <View style={styles.formIcon}>
           // Icon goes here
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 100
  },
  formRow: {
    flexDirection: 'row',
    height: 50,
  },
  formItem: {
    flex: 1,
    backgroundColor: 'dodgerblue',
  },
  formIcon: {
    width: 50,
    backgroundColor: 'greenyellow',
  },
});

Reactjs相关问答推荐

在此上下文中如何在缓冲(隐藏)视频和渲染视频之间切换?

如何通过TEK查询将对象传递到Express后台

如何使用mui为可重复使用的react 按钮应用不同的风格

有正确的方法来设置我的金牛座应用程序的图标吗?

如何在useEffect中使用useParams()

在Reaction中更新状态时,我将变得未定义

如何在MUI X数据网格列中显示IMG?

下拉Tailwind CSS菜单与怪异beviour

捕获表单数据时的Reactjs问题

React:关于useEffect钩子如何工作的困惑

状态更改时的rtk查询触发器

尽管所有页面在 Reactjs 中都是公开的,但始终导航到特定页面

如何在 React Native 中渲染下面的对象数组?

在添加了useplacesautocomplete后,在构建React/Next.js项目时,NPM抛出类型期望的错误

Select 项目后 Select HeadlessUI 组合框中的所有文本?

类型错误:类型typeof import(js-cookie)上不存在属性get

如何使用 React 在客户端获取 nestjs websocket 异常错误?

当从数组中删除数据时,UseEffect 不会重新渲染,仅在添加数据时才会重新渲染

使用输入类型文本对 useState 和日期做出react

如何在react 日历中自定义带有圆形边框的日期项?