想了解更多内容,请访问:
创新互联专注于企业成都全网营销、网站重做改版、兰考网站定制设计、自适应品牌网站建设、H5页面制作、购物商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为兰考等各大城市提供网站开发制作服务。
和华为官方合作共建的鸿蒙技术社区
https://harmonyos.
使用Java语言开发,用于Phone设备的Feature Ability模板,使用XML布局,显示分类页效果。
安装DevEco Studio,详情请参考DevEco Studio下载。
设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:
如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。
如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。
注意:'#'代表注释
功能逻辑代码
- ├─java
- │ └─com
- │ └─buty
- │ └─categoryabilityjava
- │ │ MainAbility.java
- │ │ MyApplication.java
- │ │
- │ ├─data
- │ │ CategoryData.java #用来构造分类数据
- │ │
- │ ├─model
- │ │ CategoryList.java #分类列表模型
- │ │ Item.java #子项模型
- │ │ ItemChild.java #小子项模型
- │ │ ItemList.java #子项列表模型
- │ │
- │ ├─provider
- │ │ CategoryListProvider.java #继承自BaseItemProvider为ListContainer提供数据
- │ │
- │ ├─slice
- │ │ MainAbilitySlice.java #主页片
- │ │
- │ ├─utils
- │ │ ElementUtil.java #获取element颜色值,
- │ │ LogUtils.java #日志工具类
- │ │ Toast.java #Toast工具类
- │ │
- │ └─view
- │ GridAdapter.java #grid适配器,适配不同的gridview
- │ GridView.java #自定义组件,类似grid,继承自TableLayout并且实现Component.LayoutRefreshedListener
- │
布局及样式代码
- └─resources
- ├─base
- │ ├─element
- │ │ color.json
- │ │ float.json
- │ │ string.json
- │ │
- │ ├─graphic
- │ │ background_ability_main.xml
- │ │ background_item_grid_per.xml
- │ │ background_search_bar.xml
- │ │
- │ ├─layout
- │ │ ability_main.xml #主页面
- │ │ category_list_per.xml #每个分类组件
- │ │ item_child_per.xml #分类下面的子类别
- │ │ item_child_per_grid_per.xml #子类别下的网格项组件
- │ │ item_grid_per.xml #分类下面的网格项
- │ │ search_bar.xml #search组件
- │ │ tab.xml #单个tab组件
- │ │ title_bar.xml #标题组件
- │ │
- │ ├─media
- │ │ addIcon.png
- │ │ icon.png
- │ │ icon_actived.png
- │ │ icon_normal.png
- │ │ rightIcon.png
- │ │ searchIcon.png
- │ │
- │ └─profile
- ├─en
- │ └─element
- │ string.json
- │
- ├─rawfile
- └─zh
- └─element
- string.json
我们先看一下运行起来的效果
不需要额外申请权限
页面的布局包括DependentLayout和DirectionalLayout布局。
包括ListContainer、ScrollView、自定义的GridView 等组件
还使用了include标签来导入预设的组件,导入了search组件和标题组件。
分类组件比较简单,DirectionalLayout包含一个Text。
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:bottom_margin="$float:defaultMargin"
- ohos:orientation="vertical"
- ohos:top_margin="$float:defaultMargin">
- ohos:id="$+id:categoryListPerText"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:bottom_padding="$float:componentPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:multiple_lines="true"
- ohos:text_alignment="vertical_center|left"
- ohos:text_size="16vp"
- ohos:top_padding="$float:componentPadding"/>
包含了一个自定义的GridView,我们可以学习一下如何自定义组件。
比较简单
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="60vp"
- ohos:alpha="0"
- ohos:top_margin="$float:defaultMargin">
- ohos:height="60vp"
- ohos:width="match_parent"
- ohos:background_element="$graphic:background_item_grid_per"/>
- ohos:id="$+id:itemChildPerGridPerText"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:bottom_padding="$float:componentPadding"
- ohos:multiple_lines="true"
- ohos:text_alignment="center"
- ohos:text_size="16vp"
- ohos:top_padding="$float:componentPadding"/>
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="108vp"
- ohos:background_element="$graphic:background_item_grid_per">
- ohos:id="$+id:itemGridPerImage"
- ohos:height="65vp"
- ohos:width="match_parent"/>
- ohos:id="$+id:itemGridPerText"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:align_parent_start="true"
- ohos:align_parent_top="true"
- ohos:bottom_padding="$float:componentPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:multiple_lines="true"
- ohos:start_padding="$float:defaultPadding"
- ohos:text_alignment="vertical_center"
- ohos:text_size="16vp"
- ohos:top_padding="$float:componentPadding"/>
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:background_element="$color:colorSubBackground">
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:background_element="$graphic:background_search_bar"
- ohos:bottom_margin="$float:componentPadding"
- ohos:end_margin="$float:defaultMargin"
- ohos:start_margin="$float:defaultMargin"
- ohos:top_margin="$float:componentPadding">
- ohos:id="$+id:searchImage"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:image_src="$media:searchIcon"
- ohos:start_margin="$float:defaultMargin"
- ohos:vertical_center="true"/>
- ohos:id="$+id:searchTextField"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:bottom_padding="$float:componentPadding"
- ohos:element_cursor_bubble="#00000000"
- ohos:end_of="$id:searchImage"
- ohos:end_padding="$float:defaultPadding"
- ohos:hint="$string:search"
- ohos:multiple_lines="false"
- ohos:start_padding="$float:defaultPadding"
- ohos:text_alignment="vertical_center"
- ohos:text_size="16fp"
- ohos:top_padding="$float:componentPadding"
- ohos:vertical_center="true"/>
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:id="$+id:tab"
- ohos:height="match_content"
- ohos:width="0vp"
- ohos:alignment="center"
- ohos:orientation="vertical"
- ohos:weight="1">
- ohos:id="$+id:bottom_tab_button_image"
- ohos:height="$float:heightTab"
- ohos:width="$float:widthTab"/>
- ohos:id="$+id:bottom_tab_button_text"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:text_alignment="top|horizontal_center"
- ohos:text_color="$color:colorTabTextNormal"
- ohos:text_size="$float:textSizeTab"
- ohos:top_margin="2vp"/>
两个Text 加上一个Image
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="$float:defaultHeight"
- ohos:width="match_parent"
- ohos:background_element="$color:colorSubBackground">
- ohos:id="$+id:title_text"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:align_parent_bottom="true"
- ohos:bottom_padding="$float:defaultPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:multiple_lines="true"
- ohos:start_padding="$float:defaultPadding"
- ohos:text="$string:entry_MainAbility"
- ohos:text_color="$color:appbar_title_color"
- ohos:text_size="$float:appbar_title_text_size"/>
- ohos:id="$+id:describe_text"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:align_parent_bottom="true"
- ohos:bottom_padding="$float:defaultPadding"
- ohos:end_of="$id:title_text"
- ohos:multiple_lines="true"
- ohos:text="$string:describe"
- ohos:text_color="$color:appbar_subtitle_color"
- ohos:text_size="$float:appbar_text_size"/>
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:align_parent_bottom="true"
- ohos:align_parent_end="true"
- ohos:bottom_padding="$float:defaultPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:image_src="$media:addIcon"/>
部分关键代码
- @Override
- public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
- Component itemComponent = component;
- CategoryListViewHolder viewHolder;
- //获取布局组件
- if (itemComponent == null) {
- itemComponent =
- LayoutScatter.getInstance(context)
- .parse(ResourceTable.Layout_category_list_per, componentContainer, false);
- }
- //初始化ViewHolder
- viewHolder = new CategoryListViewHolder();
- viewHolder.text = (Text) itemComponent.findComponentById(ResourceTable.Id_categoryListPerText);
- viewHolder.text.setText(getItem(index));
- //对齐方式
- if (TextTool.isLayoutRightToLeft(Locale.getDefault())) {
- viewHolder.text.setTextAlignment(TextAlignment.VERTICAL_CENTER | TextAlignment.RIGHT);
- } else {
- viewHolder.text.setTextAlignment(TextAlignment.VERTICAL_CENTER | TextAlignment.LEFT);
- }
- //获取color.json中的颜色值,设置Text组件的颜色
- if (selectIndex == index) {
- viewHolder.text.setTextColor(new Color(ElementUtil.getColor(context, ResourceTable.Color_primary_color)));
- } else {
- viewHolder.text.setTextColor(new Color(ElementUtil.getColor(context, ResourceTable.Color_primary_default)));
- }
- return itemComponent;
- }
- /**
- * 定义视图持有者
- */
- private static class CategoryListViewHolder {
- Text text;
- }
学习如何获取element color.json的颜色值,用java代码设置给组件。
- package com.buty.categoryabilityjava.utils;
- import ohos.agp.utils.Color;
- import ohos.app.Context;
- import ohos.global.resource.NotExistException;
- import ohos.global.resource.WrongTypeException;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * The ElementUtil
- */
- public class ElementUtil {
- /**
- * The getColor
- *
- * @param context context
- * @param resColorId resColorId
- * @return color
- */
- public static int getColor(Context context, int resColorId) {
- try {
- //
- String strColor = context.getResourceManager().getElement(resColorId).getString();
- if (strColor.length() == 7) {
- //返回7位颜色值
- return context.getResourceManager().getElement(resColorId).getColor();
- } else if (strColor.length() == 9) {
- //返回9位颜色值
- return Color.getIntColor(strColor);
- } else {
- return 0x000000;
- }
- } catch (WrongTypeException | NotExistException | IOException e) {
- Logger.getLogger(ElementUtil.class.getName()).log(Level.SEVERE, e.getMessage());
- }
- return 0x000000;
- }
- }
- package com.buty.categoryabilityjava.view;
- import com.buty.categoryabilityjava.utils.LogUtils;
- import ohos.agp.components.Component;
- import ohos.agp.components.Image;
- import ohos.agp.components.LayoutScatter;
- import ohos.agp.components.Text;
- import ohos.app.Context;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.IntStream;
- /**
- * The GridAdapter, adapter of the gridView
- */
- public class GridAdapter {
- private final List
componentList = new ArrayList<>(); - /**
- * The GridAdapter, adapter of the gridView
- *
- * @param context context
- * @param xmlId xmlId, the xml layout of each item of gridView
- * @param data data, the number of key-value also the number of item
- * @param from from, the collection for the key in de data
- * @param to to, the value from the data to the component target
- */
- public GridAdapter(Context context, int xmlId, List
- for (Map
datum : data) { - // get component for the xml
- Component layoutComponent = LayoutScatter.getInstance(context).parse(xmlId, null, false);
- // create each itemComponent for the args:data、from、to
- IntStream.range(0, to.length)
- .forEach(
- index -> {
- Component itemComponent = layoutComponent.findComponentById(to[index]);
- if (itemComponent instanceof Image) {
- Image image = (Image) itemComponent;
- if (datum.get(from[index]) instanceof int[]) {
- try {
- image.setPixelMap(((int[]) datum.get(from[index]))[index]);
- } catch (IndexOutOfBoundsException e) {
- LogUtils.error("GridAdapter", "IndexOutOfBoundsException");
- }
- }
- } else {
- if (itemComponent instanceof Text) {
- Text text = (Text) itemComponent;
- if (datum.get(from[index]) instanceof String[]) {
- try {
- text.setText(((String[]) datum.get(from[index]))[index]);
- } catch (IndexOutOfBoundsException e) {
- LogUtils.error("GridAdapter", "IndexOutOfBoundsException");
- text.setText("null");
- }
- } else if (datum.get(from[index]) instanceof int[]) {
- try {
- text.setText(((int[]) datum.get(from[index]))[index]);
- } catch (IndexOutOfBoundsException e) {
- LogUtils.error("GridAdapter", "IndexOutOfBoundsException");
- text.setText("null");
- }
- } else {
- text.setText("null");
- }
- }
- }
- });
- componentList.add(layoutComponent);
- }
- }
- /**
- * The getComponentList
- *
- * @return componentList
- */
- List
getComponentList() { - return componentList;
- }
- }
- package com.buty.categoryabilityjava.view;
- import ohos.agp.components.AttrSet;
- import ohos.agp.components.Component;
- import ohos.agp.components.ComponentContainer;
- import ohos.agp.components.TableLayout;
- import ohos.agp.utils.TextTool;
- import ohos.app.Context;
- import java.util.Locale;
- /**
- * The GridView, a custom component like grid
- */
- public class GridView extends TableLayout implements Component.LayoutRefreshedListener {
- private OnItemClickListener onItemClickListener;
- private OnRefreshedListener onRefreshedListener;
- private GridAdapter adapter;
- // custom Attr
- private int columnMargin;
- private int rowMargin;
- //add custom attr
- private int rowPadding;
- private boolean isOnRefreshed = false;
- public GridView(Context context) {
- super(contex
当前题目:HarmonyOS官方模板学习之Category Ability(Java)
URL标题:http://www.mswzjz.cn/qtweb/news13/482263.html攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能