HarmonyOSAPP组件分享(二)

系统 OpenHarmony
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[388212]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

Button组件介绍和应用体验分享

Button组件是常用的交互类组件之一,本节将来聊聊Button组件的使用以及按照按钮的形状,按钮可以分为:普通按钮,椭圆按钮,胶囊按钮,圆形按钮等的各种的样式图

显示效果:

代码如下:

布局中的代码:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <Button 
  9.         ohos:id="$+id:jltfbtn" 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_content" 
  12.         ohos:background_element="#FF17D3EB" 
  13.         ohos:layout_alignment="horizontal_center" 
  14.         ohos:text="我是一个按钮button" 
  15.         ohos:padding="20vp" 
  16.         ohos:top_margin="10px" 
  17.         ohos:text_color="#FFFFFF" 
  18.         ohos:text_size="50" 
  19.         /> 
  20.     <Text 
  21.         ohos:height="100px" 
  22.         ohos:width="300px" 
  23.         ohos:text_size="20fp" 
  24.         ohos:top_margin="40px" 
  25.         ohos:left_margin="400px" 
  26.         ohos:text="普通按钮"/> 
  27.     <Button 
  28.         ohos:width="200vp" 
  29.         ohos:height="70vp" 
  30.         ohos:text_size="27fp" 
  31.         ohos:text="button" 
  32.         ohos:background_element="$graphic:jltfcolor_blue_element" 
  33.         ohos:top_margin="15px" 
  34.         ohos:left_margin="90vp" 
  35.         ohos:bottom_margin="15vp" 
  36.         ohos:right_padding="8vp" 
  37.         ohos:left_padding="8vp" 
  38.         /> 
  39.     <Text 
  40.         ohos:height="100px" 
  41.         ohos:width="300px" 
  42.         ohos:text_size="20fp" 
  43.         ohos:left_margin="400px" 
  44.         ohos:text="椭圆按钮"/> 
  45.     <Button 
  46.         ohos:width="200vp" 
  47.         ohos:height="70vp" 
  48.         ohos:text_size="27fp" 
  49.         ohos:text="button" 
  50.         ohos:background_element="$graphic:jltfoval_button_element" 
  51.         ohos:bottom_margin="15vp" 
  52.         ohos:top_margin="15px" 
  53.         ohos:left_margin="90vp" 
  54.         ohos:right_padding="8vp" 
  55.         ohos:left_padding="8vp" 
  56.         /> 
  57.     <Text 
  58.         ohos:height="100px" 
  59.         ohos:width="300px" 
  60.         ohos:text_size="20fp" 
  61.         ohos:left_margin="400px" 
  62.         ohos:text="胶囊按钮"/> 
  63.     <Button 
  64.         ohos:id="$+id:button" 
  65.         ohos:width="200vp" 
  66.         ohos:height="70vp" 
  67.         ohos:text_size="27fp" 
  68.         ohos:text="button" 
  69.         ohos:background_element="$graphic:jltfcapsule_button_element" 
  70.         ohos:left_margin="90vp" 
  71.         ohos:top_margin="15px" 
  72.         ohos:bottom_margin="15vp" 
  73.         ohos:right_padding="15vp" 
  74.         ohos:left_padding="15vp" 
  75.         /> 
  76.     <Text 
  77.         ohos:height="100px" 
  78.         ohos:width="300px" 
  79.         ohos:text_size="20fp" 
  80.         ohos:left_margin="400px" 
  81.         ohos:text="圆形按钮"/> 
  82.     <Button 
  83.         ohos:id="$+id:button2" 
  84.         ohos:width="140vp" 
  85.         ohos:height="140vp" 
  86.         ohos:text_size="27fp" 
  87.         ohos:background_element="$graphic:jltfcircle_button_element" 
  88.         ohos:text="+" 
  89.         ohos:left_margin="110vp" 
  90.         ohos:bottom_margin="15vp" 
  91.         ohos:right_padding="15vp" 
  92.         ohos:left_padding="15vp" 
  93.         /> 
  94. </DirectionalLayout> 
Slice中的代码:
 
  1. package com.example.jltftiyan.slice; 
  2.  
  3. import com.example.jltftiyan.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7.  
  8. public class MainAbilitySlice extends AbilitySlice { 
  9.     @Override 
  10.     public void onStart(Intent intent) { 
  11.         super.onStart(intent); 
  12.         super.setUIContent(ResourceTable.Layout_ability_main); 
  13.  
  14.         Button button = (Button) findComponentById(ResourceTable.Id_jltfbtn); 
  15.         button.setClickedListener(l -> { 
  16.             //更改Button组件的内容 
  17.             button.setText("我被点击了~"); 
  18.         }); 
  19.     } 
  20.  
  21.     @Override 
  22.     public void onActive() { 
  23.         super.onActive(); 
  24.     } 
  25.  
  26.     @Override 
  27.     public void onForeground(Intent intent) { 
  28.         super.onForeground(intent); 
  29.     } 
graphic目录下xml文件的代码示例如下:
 
  1. jltfcolor_blue_element.xml 
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  5.        ohos:shape="rectangle"
  6.     <solid 
  7.         ohos:color="#007CFD"/> 
  8. </shape> 
  9.  
  10. jltfoval_button_element.xml 
  11.  
  12. <?xml version="1.0" encoding="utf-8"?> 
  13. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  14.        ohos:shape="oval"
  15.     <solid 
  16.         ohos:color="#007CFD"/> 
  17. </shape> 
  18.  
  19. jltfcapsule_button_element.xml 
  20.  
  21. <?xml version="1.0" encoding="utf-8"?> 
  22. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  23.        ohos:shape="rectangle"
  24.     <corners 
  25.         ohos:radius="100"/> 
  26.     <solid 
  27.         ohos:color="#007CFD"/> 
  28. </shape> 
  29.  
  30. jltfcircle_button_element.xml 
  31.  
  32. <?xml version="1.0" encoding="utf-8"?> 
  33. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  34.        ohos:shape="oval"
  35.     <solid 
  36.         ohos:color="#007CFD"/> 
  37. </shape> 

StackLayout布局练习与分享

StackLayout直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左上角,第一个添加到布局中视图显示在最底层,最后一个被放在最顶层。上一层的视图会覆盖下一层的视图。

自己可以通过修改宽高设置大小来控制位置的变换,后面会有更多新的内容呈现出来,这里就简单的敲了个案例。

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <StackLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:id="$+id:stack_layout" 
  5.     ohos:height="match_parent" 
  6.     ohos:width="match_parent"
  7.  
  8.  
  9.     <Text 
  10.         ohos:id="$+id:text_blue" 
  11.         ohos:text_alignment="bottom|right" 
  12.         ohos:text_size="24fp" 
  13.         ohos:text="第四层" 
  14.         ohos:height="match_parent" 
  15.         ohos:width="match_parent" 
  16.         ohos:background_element="#3F56EA" /> 
  17.  
  18.  
  19.     <Text 
  20.         ohos:id="$+id:text_light_purple" 
  21.         ohos:text_alignment="bottom|right" 
  22.         ohos:text_size="24fp" 
  23.         ohos:text="第三层" 
  24.         ohos:height="300vp" 
  25.         ohos:width="match_parent" 
  26.         ohos:background_element="#00AAEE" /> 
  27.     <Text 
  28.         ohos:id="$+id:text_light_lianxi" 
  29.         ohos:text_alignment="bottom|horizontal_center" 
  30.         ohos:text_size="24fp" 
  31.         ohos:text="第二层" 
  32.         ohos:height="match_parent" 
  33.         ohos:width="150vp" 
  34.         ohos:background_element="#FF74FF8B" /> 
  35.  
  36.  
  37.     <Text 
  38.         ohos:id="$+id:text_orange" 
  39.         ohos:text_alignment="center" 
  40.         ohos:text_size="24fp" 
  41.         ohos:text="第一层" 
  42.         ohos:height="80vp" 
  43.         ohos:width="match_parent" 
  44.         ohos:background_element="#00BFC9" /> 
  45.  
  46. </StackLayout> 
  47.  
  48.   

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-03-22 09:48:32

鸿蒙HarmonyOS应用开发

2021-03-26 09:35:35

鸿蒙HarmonyOS应用开发

2021-03-31 15:49:34

鸿蒙HarmonyOS应用

2021-03-17 09:35:09

鸿蒙HarmonyOS应用开发

2021-03-30 09:45:07

鸿蒙HarmonyOS应用开发

2010-07-28 12:41:18

Flex组件

2017-01-19 18:58:11

iOS组件化方案

2011-03-31 13:52:22

Java

2012-11-12 09:41:31

Scrum敏捷开发开发培训

2021-10-14 15:14:36

鸿蒙HarmonyOS应用

2021-09-07 09:53:45

鸿蒙HarmonyOS应用

2012-04-28 21:25:58

APP

2011-07-13 16:48:55

CC++

2010-08-06 11:19:24

FlexPaperFlex

2023-04-02 10:06:24

组件vue3sign2.

2022-02-17 20:07:45

Flex鸿蒙Flex组件

2009-12-16 10:49:42

Ruby操作二进制文件

2012-11-07 10:01:52

组件技术OAuth授权登陆

2012-11-07 10:09:11

组件技术OAuth授权登陆

2021-08-02 14:54:50

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号