我的代码没有给我预期的设计

发布于 2025-01-10 17:49:25 字数 3417 浏览 0 评论 0原文

我想制作提要屏幕,这是每个帖子项目的我想要实现的目标。 这是我目前拥有的。我认为这可以完成工作。我是否在某个地方犯了错误,或者我需要在样式中添加其他内容?我在设计上遇到了困难,并认为这足以构建组件。例如,我期望图像是顶部元素,但不知何故它位于中间......另外,当我遇到此类问题时,如何应对它们,有什么建议吗?





        <View style={styles.wrapper}>
         <Opacity onPress={props.onPostSelect}>
           <View style={{ ...styles.postContainer, ...props.style }}>
             {/* thumbnail  */}
             <View style={styles.thumbnailContainer}>
               <Image
                 resizeMode="contain"
                 source={{ uri: props.item.thumbnailURL }}
                 style={styles.thumbnail}
               />
             </View>
             {/* thumbnail end*/}

             {/* info container */}
             <View style={styles.infoContainer}>
               <View style={styles.postDetailsContainer}>
                 {/* //TITLE */}
                 <View style={styles.title}>
                   <Text style={styles.text} numberOfLines={1}>
                     {props.item.title}
                   </Text>
                 </View>
                 {/* //TITLE end*/}

                 {/* LIKES/VIEWS */}
                 <View style={styles.likesAndViewsContainer}>
                   <Text style={styles.text}>{props.item.views} views | </Text>
                   {/* LIKES */}
                   <View style={styles.likesContainer}>
                     <MaterialIcons name="thumb-up" color="white" size={18} />
                     <Text style={styles.text}> {props.item.likes} </Text>
                   </View>
                 </View>
               </View>
             </View>
           </View>
         </Opacity>
       </View>

风格:


     const styles = StyleSheet.create({
      postContainer: {
        justifyContent: "center",
        borderWidth: 7,
        borderColor: "brown",
        borderRadius: 2,
        backgroundColor: "pink",
        marginHorizontal: 5,
        height: "95%",
        overflow: "hidden",
        flexDirection: "column",

        justifyContent: "center",
        alignItems: "center",
      },
      wrapper: {
        flex: 1,
        flexGrow: 1,
        height: 500,
        width: "100%",
        backgroundColor: Colors.secondaryColors.lightGray,
        overflow: "hidden",
      },
      title: {
        flexDirection: "column",
        color: "white",
        height: "80%",
      },
      likesAndViewsContainer: {
        flexDirection: "row",
        justifyContent: "flex-start",
        overflow: "hidden",
      },
      text: {color:black},
      likesContainer: {
        flexDirection: "row",
      },
      thumbnail: {
        flex: 1,
        width: 100,
        height: "100%",
        position: "absolute",
      },
      thumbnailContainer: {
        flex: 1,
        height: "100%",
        width: "100%",
        marginVertical: 10,
        position: "absolute",
        right: 10,
        left: 10,
      },
      infoContainer: {
        flexDirection: "row",
      },
      postDetailsContainer: {
        color: "white",
        fontSize: 16,
      flexDirection: "column",
      },
    });

I want to make feed screen and this is what i want to achieve for every post item.
This is what i have currently . I thought this will do the work. Am I making a mistake somewhere or I need to add someting else to the styles? I struggle with design and thought this will be enough to structure the components. For example, I expected the image to be the top element but somehow it is in the middle... Also, when I have such problems, how to cope with them, any tips?





        <View style={styles.wrapper}>
         <Opacity onPress={props.onPostSelect}>
           <View style={{ ...styles.postContainer, ...props.style }}>
             {/* thumbnail  */}
             <View style={styles.thumbnailContainer}>
               <Image
                 resizeMode="contain"
                 source={{ uri: props.item.thumbnailURL }}
                 style={styles.thumbnail}
               />
             </View>
             {/* thumbnail end*/}

             {/* info container */}
             <View style={styles.infoContainer}>
               <View style={styles.postDetailsContainer}>
                 {/* //TITLE */}
                 <View style={styles.title}>
                   <Text style={styles.text} numberOfLines={1}>
                     {props.item.title}
                   </Text>
                 </View>
                 {/* //TITLE end*/}

                 {/* LIKES/VIEWS */}
                 <View style={styles.likesAndViewsContainer}>
                   <Text style={styles.text}>{props.item.views} views | </Text>
                   {/* LIKES */}
                   <View style={styles.likesContainer}>
                     <MaterialIcons name="thumb-up" color="white" size={18} />
                     <Text style={styles.text}> {props.item.likes} </Text>
                   </View>
                 </View>
               </View>
             </View>
           </View>
         </Opacity>
       </View>

the styles :


     const styles = StyleSheet.create({
      postContainer: {
        justifyContent: "center",
        borderWidth: 7,
        borderColor: "brown",
        borderRadius: 2,
        backgroundColor: "pink",
        marginHorizontal: 5,
        height: "95%",
        overflow: "hidden",
        flexDirection: "column",

        justifyContent: "center",
        alignItems: "center",
      },
      wrapper: {
        flex: 1,
        flexGrow: 1,
        height: 500,
        width: "100%",
        backgroundColor: Colors.secondaryColors.lightGray,
        overflow: "hidden",
      },
      title: {
        flexDirection: "column",
        color: "white",
        height: "80%",
      },
      likesAndViewsContainer: {
        flexDirection: "row",
        justifyContent: "flex-start",
        overflow: "hidden",
      },
      text: {color:black},
      likesContainer: {
        flexDirection: "row",
      },
      thumbnail: {
        flex: 1,
        width: 100,
        height: "100%",
        position: "absolute",
      },
      thumbnailContainer: {
        flex: 1,
        height: "100%",
        width: "100%",
        marginVertical: 10,
        position: "absolute",
        right: 10,
        left: 10,
      },
      infoContainer: {
        flexDirection: "row",
      },
      postDetailsContainer: {
        color: "white",
        fontSize: 16,
      flexDirection: "column",
      },
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2025-01-17 17:49:25

我认为这与您的 position:'absolute'flex:1 有关。
我已经把它清理了一下,请告诉我,这是否是您想要的

<View style={styles.wrapper}>
      <View style={styles.postContainer}>
        <View style={styles.thumbnailContainer}>
          <Image
            resizeMode="contain"
            source={{ uri: props.item.thumbnailURL }} 
            style={styles.thumbnail}
          />
        </View>
        <View style={styles.infoContainer}>
          <Text style={styles.text} numberOfLines={1}>{props.item.title}</Text>
          <View style={styles.likesAndViewsContainer}>
            <Text style={styles.text}>{props.item.view} views | </Text>
            <View style={styles.likesContainer}>
              <MaterialIcons name="thumb-up" color="white" size={18} />
              <Text style={styles.text}> {props.item.likes} </Text>
            </View>
          </View>
        </View>
      </View>
    </View>

样式:

const styles = StyleSheet.create({
  postContainer: {
    borderWidth: 1,
    borderColor: "brown",
    backgroundColor: "pink",
    padding:15,
  },
  wrapper: {
    flex:1,
    backgroundColor: 'gray',
  },
  likesAndViewsContainer: {
    flexDirection: "row",
    justifyContent: "flex-start",
  },
  text: {
    color:'black',
    marginBottom:15,
    fontWeight:'bold',
    },
  likesContainer: {
    flexDirection: "row",
  },
  thumbnailContainer:{
    height: 100,
    width:100,
    borderWidth:1,
    alignSelf:'center',

  },
  thumbnail: {
    height: 100,
    width:100,
  },
  infoContainer: {
    marginTop:15,
  },
     
});

i think there's something to do with your position:'absolute' and flex:1.
I've cleaned it up a bit, and do tell me, if this is what you're going for

<View style={styles.wrapper}>
      <View style={styles.postContainer}>
        <View style={styles.thumbnailContainer}>
          <Image
            resizeMode="contain"
            source={{ uri: props.item.thumbnailURL }} 
            style={styles.thumbnail}
          />
        </View>
        <View style={styles.infoContainer}>
          <Text style={styles.text} numberOfLines={1}>{props.item.title}</Text>
          <View style={styles.likesAndViewsContainer}>
            <Text style={styles.text}>{props.item.view} views | </Text>
            <View style={styles.likesContainer}>
              <MaterialIcons name="thumb-up" color="white" size={18} />
              <Text style={styles.text}> {props.item.likes} </Text>
            </View>
          </View>
        </View>
      </View>
    </View>

the styles:

const styles = StyleSheet.create({
  postContainer: {
    borderWidth: 1,
    borderColor: "brown",
    backgroundColor: "pink",
    padding:15,
  },
  wrapper: {
    flex:1,
    backgroundColor: 'gray',
  },
  likesAndViewsContainer: {
    flexDirection: "row",
    justifyContent: "flex-start",
  },
  text: {
    color:'black',
    marginBottom:15,
    fontWeight:'bold',
    },
  likesContainer: {
    flexDirection: "row",
  },
  thumbnailContainer:{
    height: 100,
    width:100,
    borderWidth:1,
    alignSelf:'center',

  },
  thumbnail: {
    height: 100,
    width:100,
  },
  infoContainer: {
    marginTop:15,
  },
     
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文