当前位置:   article > 正文

【鸿蒙】HarMonyOS的UI组件学习一_鸿蒙background_element

鸿蒙background_element

打开DevEco studio,选择java语言新建项目

打开Layout布局文件夹,编辑布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DependentLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:background_element="#ffffff">
  7. <Image
  8. ohos:id="$+id:image"
  9. ohos:height="80fp"
  10. ohos:width="80fp"
  11. ohos:scale_mode="stretch"
  12. ohos:image_src="$media:hw"
  13. ohos:center_in_parent="true"/>
  14. <Text
  15. ohos:id="$+id:text"
  16. ohos:width="match_content"
  17. ohos:height="match_content"
  18. ohos:text="$string:mainability_HelloWorld"
  19. ohos:text_color="#ff0000"
  20. ohos:below="$id:image"
  21. ohos:text_size="32fp"
  22. ohos:center_in_parent="true"/>
  23. <Button
  24. ohos:id="$+id:button"
  25. ohos:width="match_content"
  26. ohos:height="match_content"
  27. ohos:text="Next"
  28. ohos:text_size="19fp"
  29. ohos:text_color="#FFFFFF"
  30. ohos:top_padding="8vp"
  31. ohos:bottom_padding="8vp"
  32. ohos:right_padding="70vp"
  33. ohos:left_padding="70vp"
  34. ohos:center_in_parent="true"
  35. ohos:below="$id:text"
  36. ohos:margin="10vp"
  37. ohos:background_element="$graphic:background_button"
  38. />
  39. </DependentLayout>

更改按钮的样式,将按钮的形状改为矩形,设置圆角以及背景颜色

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:shape="rectangle">
  5. <corners
  6. ohos:radius="10"
  7. />
  8. <solid
  9. ohos:color="#ff0000"/>
  10. </shape>

界面效果为:

在MainAbilitySilce.java类中获得Button控件的对象,添加点击事件,完成页面跳转。

  1. package com.example.hm_phone_java.slice;
  2. import com.example.hm_phone_java.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Button;
  6. import ohos.agp.components.Component;
  7. public class MainAbilitySlice extends AbilitySlice {
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_ability_main);
  12. //根据按钮的id将布局中的控件获得出来
  13. Button btn= (Button) this.findComponentById(ResourceTable.Id_button);
  14. //给按钮添加点击事件,使用匿名内部类方式跳转页面
  15. btn.setClickedListener(new Component.ClickedListener() {
  16. @Override
  17. public void onClick(Component component) {
  18. //跳转页面
  19. present(new ThreeAbilitySlice(),new Intent());
  20. }
  21. });
  22. }
  23. @Override
  24. public void onActive() {
  25. super.onActive();
  26. }
  27. @Override
  28. public void onForeground(Intent intent) {
  29. super.onForeground(intent);
  30. }
  31. }

通过触发“next”按钮的点击时间跳转至ThreeAbilitySlice.java界面,在ThreeAbilitySlice界面上加载布局文件,ThreeAbilitySlice界面的布局代码如下:

  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. <!--DirectionalLayout:线性布局
  8. DependentLayout:相对布局
  9. PositionLayout:绝对布局
  10. TableLayout:表格布局-->
  11. <DirectionalLayout
  12. ohos:height="1170px"
  13. ohos:width="match_parent"
  14. ohos:alignment="center"
  15. ohos:background_element="#ffff00"
  16. ohos:orientation="vertical">
  17. <Button
  18. ohos:height="match_content"
  19. ohos:width="match_content"
  20. ohos:background_element="$graphic:background_button"
  21. ohos:bottom_padding="10px"
  22. ohos:left_padding="50px"
  23. ohos:right_padding="50px"
  24. ohos:text="$string:btnName"
  25. ohos:text_color="#FFFFFF"
  26. ohos:text_size="60px"
  27. ohos:top_padding="10px"/>
  28. <Button
  29. ohos:height="match_content"
  30. ohos:width="match_content"
  31. ohos:background_element="$graphic:background_button1"
  32. ohos:bottom_padding="10px"
  33. ohos:left_padding="50px"
  34. ohos:right_padding="50px"
  35. ohos:text="$string:btnName"
  36. ohos:text_color="#FFFFFF"
  37. ohos:text_size="80px"
  38. ohos:top_margin="10px"
  39. ohos:top_padding="10px"/>
  40. <Button
  41. ohos:height="match_content"
  42. ohos:width="match_content"
  43. ohos:background_element="$graphic:background_button2"
  44. ohos:bottom_padding="10px"
  45. ohos:left_padding="50px"
  46. ohos:right_padding="50px"
  47. ohos:text="$string:btnName"
  48. ohos:text_color="#FFFFFF"
  49. ohos:text_size="100px"
  50. ohos:top_margin="10px"
  51. ohos:top_padding="10px"/>
  52. <Button
  53. ohos:height="match_content"
  54. ohos:width="match_content"
  55. ohos:background_element="$graphic:background_button3"
  56. ohos:bottom_padding="10px"
  57. ohos:left_padding="50px"
  58. ohos:right_padding="50px"
  59. ohos:text="$string:btnName"
  60. ohos:text_color="#FFFFFF"
  61. ohos:text_size="120px"
  62. ohos:top_margin="10px"
  63. ohos:top_padding="10px"/>
  64. </DirectionalLayout>
  65. <DependentLayout
  66. ohos:height="1170px"
  67. ohos:width="match_parent"
  68. ohos:background_element="#00ffff">
  69. <Button
  70. ohos:id="$+id:btn1"
  71. ohos:height="match_content"
  72. ohos:width="match_content"
  73. ohos:background_element="$graphic:background_button"
  74. ohos:bottom_padding="10px"
  75. ohos:left_padding="50px"
  76. ohos:right_padding="50px"
  77. ohos:text="$string:btnName"
  78. ohos:center_in_parent="true"
  79. ohos:text_color="#FFFFFF"
  80. ohos:text_size="60px"
  81. ohos:top_padding="10px"/>
  82. <Button
  83. ohos:id="$+id:btn2"
  84. ohos:height="match_content"
  85. ohos:width="match_content"
  86. ohos:text="$string:btnName"
  87. ohos:above="$id:btn1"
  88. ohos:align_left="$id:btn1"
  89. ohos:bottom_margin="10px"
  90. ohos:left_padding="50px"
  91. ohos:right_padding="50px"
  92. ohos:top_padding="10px"
  93. ohos:bottom_padding="10px"
  94. ohos:text_color="#FFFFFF"
  95. ohos:text_size="60px"
  96. ohos:background_element="$graphic:background_button4"/>
  97. <Button
  98. ohos:id="$+id:btn3"
  99. ohos:height="match_content"
  100. ohos:width="match_content"
  101. ohos:text="$string:btnName"
  102. ohos:left_of="$id:btn1"
  103. ohos:align_top="$id:btn1"
  104. ohos:right_margin="10px"
  105. ohos:left_padding="50px"
  106. ohos:right_padding="50px"
  107. ohos:top_padding="10px"
  108. ohos:bottom_padding="10px"
  109. ohos:text_color="#FFFFFF"
  110. ohos:text_size="60px"
  111. ohos:background_element="$graphic:background_button1"/>
  112. <Button
  113. ohos:id="$+id:btn4"
  114. ohos:height="match_content"
  115. ohos:width="match_content"
  116. ohos:text="$string:btnName"
  117. ohos:below="$id:btn1"
  118. ohos:align_left="$id:btn1"
  119. ohos:top_margin="10px"
  120. ohos:left_padding="50px"
  121. ohos:right_padding="50px"
  122. ohos:top_padding="10px"
  123. ohos:bottom_padding="10px"
  124. ohos:text_color="#FFFFFF"
  125. ohos:text_size="60px"
  126. ohos:background_element="$graphic:background_button2"/>
  127. <Button
  128. ohos:id="$+id:btn5"
  129. ohos:height="match_content"
  130. ohos:width="match_content"
  131. ohos:text="$string:btnName"
  132. ohos:right_of="$id:btn1"
  133. ohos:align_top="$id:btn1"
  134. ohos:left_margin="10px"
  135. ohos:left_padding="50px"
  136. ohos:right_padding="50px"
  137. ohos:top_padding="10px"
  138. ohos:bottom_padding="10px"
  139. ohos:text_color="#FFFFFF"
  140. ohos:text_size="60px"
  141. ohos:background_element="$graphic:background_button3"/>
  142. </DependentLayout>
  143. </DirectionalLayout>

界面效果如下:

该界面整体使用DirectionalLayout线性布局,方向为垂直摆放,里面分为两部分,上部分使用DirectionalLayout线性布局,内部组件进行居中对齐显示,下半部分使用DependentLayout相对布局摆放控件,使按钮根据中心按钮的相对位置进行摆放。

  1. package com.example.hm_phone_java.slice;
  2. import com.example.hm_phone_java.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.colors.RgbColor;
  6. import ohos.agp.components.Button;
  7. import ohos.agp.components.Component;
  8. import ohos.agp.components.element.ShapeElement;
  9. public class ThreeAbilitySlice extends AbilitySlice {
  10. private final RgbColor[] colors={
  11. new RgbColor(255,0,0),
  12. new RgbColor(0,255,0),
  13. new RgbColor(0,0,255),
  14. new RgbColor(255,255,0),
  15. new RgbColor(255,0,255),
  16. };
  17. private int num=0;
  18. @Override
  19. protected void onStart(Intent intent) {
  20. super.onStart(intent);
  21. super.setUIContent(ResourceTable.Layout_ability_three);
  22. Button btn1= (Button) this.findComponentById(ResourceTable.Id_btn1);
  23. btn1.setClickedListener(new Component.ClickedListener() {
  24. @Override
  25. public void onClick(Component component) {
  26. ShapeElement se=new ShapeElement();
  27. se.setRgbColor(colors[++num%colors.length]);
  28. btn1.setBackground(se);
  29. }
  30. });
  31. }
  32. }

在java代码中对下半部分的中心的按钮获得对象,并实现该按钮的点击事件,每点击该按钮一次,使它的背景颜色发生改变,进行循环的改变五种颜色。

点击Tool开启华为模拟器,在华为开发者网站上完成实名登录,

同意允许协议

接下来你会看到这个页面,说明可以使用华为模拟器运行了

同时,DevEco Studio上会显示模拟器选择界面,

开启华为P40模拟器,将项目运行即可。

下一篇 【鸿蒙】HarMonyOS的UI组件学习二之拨号界面

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签