首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从数组获取时如何使项位置保持静态

从数组获取时如何使项位置保持静态
EN

Stack Overflow用户
提问于 2021-08-12 04:12:08
回答 1查看 29关注 0票数 0

目前,我有这样一个数组

代码语言:javascript
复制
const HeaderData = [
  {
    mainData: [
      {
        title: "Created On",
        detail: "4/1/2021",
      },
      {
        title: "Created By",
        detail: "Jennifer. 0",
      },
      {
        title: "Started",
        detail: "4/1/2021",
      },
      {
        title: "Completed",
        detail: "In progress",
      },
    ],
  },
  {
    activityName: [
      {
        title: "Take the stairs",
        notSatisfied: 5,
        neutral: 0,
        satisfied: 2,
      },
      {
        title: "5 Sets of Sit-Ups",
        notSatisfied: 3,
        neutral: 2,
        satisfied: 5,
      },
      {
        title: "3 sets of Push-Up",
        notSatisfied: 7,
        neutral: 3,
        satisfied: 1,
      },
      {
        title: "Weight-lifting",
        notSatisfied: 3,
        neutral: 2,
        satisfied: 0,
      },
    ],
  },
]

我拿数据来做这样的事情

但我只有这样的东西

以下是代码:

代码语言:javascript
复制
        <View>
            {HeaderData.filter((elm) => elm.activityName).map((item, key) => (
              <View key={key} row centerV>
                <View>
                  {item.activityName.map((itemD, key) => (
                    <View key={key} row centerV>
                      <View padding-10>
                        <Text preset="h4">{itemD.title}</Text>
                        <Text preset="h6" style={{ color: color.primary }} >About>> </Text>
                      </View>
                      <View key={key} >
                        <Text preset="h4">{itemD.notSatisfied}</Text>
                      </View>
                    </View>
                  ))}
                </View>

              </View>
            ))}
          </View>

我试着把数字放在外面,但问题是我不知道如何使数字匹配文本,平均数字将是同一行文本,但我得到了这个

因此,如何使数字与文本相同,并且它们位于同一行?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-12 08:27:30

您可以使用下面的内容。最关键的部分是创建“装箱”视图字段,并将flexDirection: "row"用于您希望水平表示的元素。

JSX

代码语言:javascript
复制
<View style={styles.container}>
  {HeaderData.filter((elm) => elm.activityName).map((item, key) => (
    item.activityName.map((itemD, key) => (
      // The main container to hold "each" item. Important to set "flexDirection: 'row'" 
      <View key={key} style={styles.itemContainer}>
        {/* LEFT_SIDE View for the item titles and 'About' text. Set "flexDirection: 'column'", which is the default.  */}
        <View style={styles.itemLabelContainer}>
          <Text style={styles.itemTitle}>{itemD.title}</Text>
          <Text style={styles.about}>
            About>> 
          </Text>
        </View>
        {/* RIGHT_SIDE View for the item values. Again, set "flexDirection: 'row'" since the values are presented horizontally. */}
        <View style={styles.itemValueContainer}>
          <Text style={styles.itemValue}>{itemD.notSatisfied}</Text>
          <Text style={styles.itemValue}>{itemD.neutral}</Text>
          <Text style={styles.itemValue}>{itemD.satisfied}</Text>
        </View>
      </View>
    ))
  ))}
</View>

样式

代码语言:javascript
复制
const styles = StyleSheet.create({
  itemContainer: {
    flexDirection: 'row',
    paddingHorizontal: 20,
    paddingVertical: 12,
    justifyContent: 'space-between',
  },
  itemTitle: {
    fontWeight: 'bold'
  },
  about: {
    color: 'blue',
    paddingVertical: 4,
  },
  itemLabelContainer: {
    flexDirection: 'column',
    marginRight: 20,
  },
  itemValueContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  itemValue: {
    paddingHorizontal: 4,
    textAlignVertical: 'center',
  },

  container: {
    flex: 1,
    width: '100%',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68751504

复制
相关文章

相似问题

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