HarmonyOS自学-Day5(使用List、Stack、RelativeContainer相关组件实现的小案例)


文章声明⭐⭐⭐

  1. 该文章为我(有编程语言基础,非编程小白)的 HarmonyOS自学笔记,此类文章笔记我会默认大家都学过前端相关的知识,并常常以实现相关小案例的形式记录自己的学习过程
  2. 知识来源为 HarmonyOS官方文档/B站UP主筱筱知晓的HarmonyOS教学视频,归纳为自己的语言与理解记录于此
  3. 不出意外的话,我大抵会 持续更新
  4. 想要了解前端开发(技术栈大致有:Vue2/3、微信小程序、uniapp、HarmonyOS、NodeJS、Typescript)与Python的小伙伴,可以关注我!谢谢大家!

让我们开始今天的学习吧!

小案例

代码如下:

@Entry
@Component
struct Hello {
  // 推荐列表
  @State hotSearchList: string[] = ['女装', '鞋子', '军大衣', '花棉袄', 'Flutter教程', 'HarmonyOS', 'ArkTS', '高跟鞋', '电脑', '华为手机']
  // 控制按钮显示的布尔值
  @State isShow: boolean = false

  build() {
    // 堆叠布局
    Stack({ alignContent: Alignment.BottomEnd }) {
      // 标题与滑动列表
      Column() {
        // 标题
        Text('精品推荐')
          .width('100%')
          .height(60)
          .fontSize(40)
          .textAlign(TextAlign.Center)
        // 滑动列表
        List() {
          ForEach(this.hotSearchList, item => {
            ListItem() {
              Text(`${item}`) {
              }
              .width('100%')
              .height(60)
              .fontSize(25)
              .textAlign(TextAlign.Center)
              .backgroundColor('#eee')
              .margin({ top: 20 })
              .borderRadius(10)
            }
          }, (item, index) => item + `${index}`)
        }
        .layoutWeight(1)
      }
      .width('100%')
      .height('100%')
      // 相对定位容器
      RelativeContainer() {
        // 主按钮
        Button() {
          Text('A')
            .fontSize(26)
            .textAlign(TextAlign.Center)
            .fontColor(Color.White)
        }
        .id('mainButton')
        .width(60)
        .height(60)
        .backgroundColor('#4db8de')
        .alignRules({
          bottom: {
            anchor: '__container__',
            align: VerticalAlign.Bottom
          },
          right: {
            anchor: '__container__',
            align: HorizontalAlign.End
          }
        })
        .onClick(() => {
          // 控制是否显示多个副按钮
          this.isShow = !this.isShow
        })
        // 动态控制显示多个副按钮
        if (this.isShow) {
          Button() {
            Text('B')
              .fontSize(26)
              .textAlign(TextAlign.Center)
              .fontColor(Color.White)
          }
          .id('ButtonB')
          .width(60)
          .height(60)
          .backgroundColor(Color.Orange)
          .alignRules({
            bottom: {
              anchor: 'mainButton',
              align: VerticalAlign.Top
            },
            right: {
              anchor: '__container__',
              align: HorizontalAlign.End
            }
          })
          .offset({
            y: -25
          })

          Button() {
            Text('C')
              .fontSize(26)
              .textAlign(TextAlign.Center)
              .fontColor(Color.White)
          }
          .id('ButtonC')
          .width(60)
          .height(60)
          .backgroundColor(Color.Red)
          .alignRules({
            bottom: {
              anchor: 'mainButton',
              align: VerticalAlign.Top
            },
            right: {
              anchor: 'mainButton',
              align: HorizontalAlign.Start
            }
          })

          Button() {
            Text('D')
              .fontSize(26)
              .textAlign(TextAlign.Center)
              .fontColor(Color.White)
          }
          .id('ButtonD')
          .width(60)
          .height(60)
          .backgroundColor(Color.Green)
          .alignRules({
            bottom: {
              anchor: '__container__',
              align: VerticalAlign.Bottom
            },
            left: {
              anchor: '__container__',
              align: HorizontalAlign.Start
            }
          })
          .offset({
            x: 5
          })
        }
      }
      .width(150)
      .height(150)
    }
    .width('100%')
    .height('100%')
    .padding(15)
  }
}

效果展示:
效果展示


  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我了解你的问题了。首先,我们需要使用 React Native 中的 `Modal` 组件实现弹出模态框的效果。然后,我们可以在 `Modal` 组件中嵌套一个 `View` 组件,并在该 `View` 组件使用 `Stack` 组件实现向上弹出的效果。 以下是实现的步骤: 1. 导入必要的组件: ``` import React, { useState } from 'react'; import { View, TouchableOpacity, Text, Modal } from 'react-native'; import { createStackNavigator } from '@react-navigation/stack'; ``` 2. 创建一个 `MyModal` 组件,用于渲染模态框内容: ``` const MyModal = () => { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>这是一个模态框</Text> </View> ); }; ``` 3. 创建一个包含 `MyModal` 组件的 `Stack` 组件: ``` const Stack = createStackNavigator(); const MyStack = () => { return ( <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} options={{ title: 'Home' }} /> <Stack.Screen name="Modal" component={MyModal} /> </Stack.Navigator> ); }; ``` 4. 在 `HomeScreen` 组件中,使用 `Modal` 组件来包裹 `MyStack` 组件,并在 `MyStack` 组件使用 `TouchableOpacity` 组件来触发模态框的显示: ``` const HomeScreen = ({ navigation }) => { const [modalVisible, setModalVisible] = useState(false); return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <TouchableOpacity onPress={() => setModalVisible(true)}> <Text>打开模态框</Text> </TouchableOpacity> <Modal animationType="slide" transparent visible={modalVisible}> <MyStack /> </Modal> </View> ); }; ``` 5. 最后,将 `MyStack` 组件作为根组件进行渲染: ``` const App = () => { return ( <NavigationContainer> <MyStack /> </NavigationContainer> ); }; export default App; ``` 这样,我们就成功地实现了模态框向上弹出的效果。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯丢匹德先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值