当前位置:网站首页>OpenHarmony资源管理详解
OpenHarmony资源管理详解
2022-07-05 00:01:00 【InfoQ】
资源分类
resourcesresouresbaserawfile
baseelementmediaanimationlayoutgraphicprofile
资源组目录
限定词目录
- 在为设备匹配对应的资源文件时,限定词目录匹配的优先级从高到低依次为:移动国家码和移动网络码 > 区域(可选组合:语言、语言_文字、语言_国家或地区、语言_文字_国家或地区)> 横竖屏 > 设备类型 > 颜色模式 > 屏幕密度。
- 如果限定词目录中包含移动国家码和移动网络码、语言、文字、横竖屏、设备类型、颜色模式限定词,则对应限定词的取值必须与当前的设备状态完全一致,该目录才能够参与设备的资源匹配。例如,限定词目录“zh_CN-car-ldpi”不能参与“en_US”设备的资源匹配。
资源访问
- 访问应用资源目录
base目录下的资源文件会被编译成二进制文件并且给这些资源赋予唯一的 ID ,使用相应资源的时候通过资源访问符$('app.type.name')的形式,app代表是应用内resources目录中定义的资源;type表示资源类型,可取值有color、float、string、string、media等;name表示资源的文件名字。例如media中新加name为 Car.svg的图片,则访问该字符串资源为$r('app.media.Car')。
- 笔者在
base目录下新建string.json和color.json文件,分别存放字符串和颜色,资源内容如下图所示:

- 通过$('app.type.name')访问资源的简单样例如下所示:
@Entry
@Component
struct Index {
build() {
Row() {
Column() {
Text($r('app.string.title_desc')) // 访问字符串资源
.fontSize(60).fontWeight(FontWeight.Bold)
.fontColor($r('app.color.title_color')) // 访问字体颜色
.backgroundImage($r('app.media.Car')) // 设备背景图片
Image("common/images/Car.svg").objectFit(ImageFit.Contain).height(200)
}
.width('100%')
}
.height('100%')
}
}

- 访问系统资源
- 系统资源包含
颜色、圆角、字体、间距、字符串及图片等,通过使用系统资源,不同的开发者可以开发出具有相同视觉风格的应用,开发者可以通过$r('sys.type.name')的形式引用系统资源,和访问应用资源不同的是使用sys代表系统资源,其它和访问应用资源规则一致。
- 访问系统资源简单样例如下所示:
@Entry
@Component
struct ResourceTest {
build() {
Column() {
Text($r('app.string.title_desc')) //**访问应用资源目录**
.fontColor($r('sys.color.ohos_fa_alert')) //**访问系统资源目录**
.fontSize($r('sys.float.ohos_id_text_size_headline3'))
.backgroundColor($r('sys.color.ohos_id_color_palette_aux1'))
Image("/common/images/Car.svg") //**自己创建的目录**
.objectFit(ImageFit.None)
.border({
color: Color.Orange,
radius: 20,
width: 12
})
.margin({
top: 50,
})
.width(200)
.height(200)
}
.padding(20)
.width("100%")
.height("100%")
}
}

Image("/common/images/Car.svg") //**自己创建的目录**
.objectFit(ImageFit.None)
.border({
color: Color.Orange,
radius: 20,
width: 12
})
.margin({
top: 50,
})
.width(200)
.height(200)

资源管理器
@ohos.resourceManagerResourceManagerresourceManagerdeclare namespace resourceManager {
// 获取ResourceManager
export function getResourceManager(callback: AsyncCallback<ResourceManager>): void;
// 获取指定bundleName的ResourceManager
export function getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void;
export interface ResourceManager {
// 获取字符串资源
getString(resId: number, callback: AsyncCallback<string>): void;
// 获取字符串数组资源
getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void;
// 获取媒体资源
getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void;
// 获取设备信息,比如当前屏幕密度,设备类型是手机还是平板等
getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void;
// 获取配置信息,比如当前屏幕方向密度,当前设备语言
getConfiguration(callback: AsyncCallback<Configuration>): void;
// 释放ResourceManager资源
release();
}
}
export default resourceManager;
ResourceManagergetResourceManager()ResourceManagergetXXX()ResourceManager- 引入 resourceManager
import resourceManager from '@ohos.resourceManager';
- 获取 ResourceManager
aboutToAppear() {
resourceManager.getResourceManager((error, manager) => {
// 获取manager
})
}
- 使用 ResourceManager
manager.getString(0x1000001, (innerError, data) => {
if(data) {
// 获取资源成功
} else {
console.log("error: " + JSON.stringify(innerError))
}
})
import resourceManager from '@ohos.resourceManager';
@Entry @Component struct ResourceTest {
@State text_string: string = "跟着坚果学习";
@State capability: string = "OpenHarmony";
@State configuration: string = "应用开发";
aboutToAppear() {
resourceManager.getResourceManager((error, manager) => {
manager.getString(0x1000001, (innerError, data) => {
if(data) {
this.text_string = data;
} else {
console.log("error: " + JSON.stringify(innerError));
}
})
manager.getDeviceCapability((innerError, deviceCapability) => {
if(deviceCapability) {
this.capability = JSON.stringify(deviceCapability);
}
})
manager.getConfiguration((innerError, configuration) => {
if(configuration) {
this.configuration = JSON.stringify(configuration);
}
})
})
}
build() {
Column({ }) {
Text(this.text_string) // 访问字符串资源
// 设置尺寸
.fontSize(29)
.fontColor($r('app.color.title_color')) // 访问字体颜色
Text(this.capability) // capability信息
.fontSize(40).fontWeight(FontWeight.Bold)
Text(this.configuration) // configuration信息
.fontSize(60)
}
.width('100%')
.height('100%')
.padding(10)
}
}

边栏推荐
- 基于三维gis平台的消防系统运用
- QT addition calculator (simple case)
- Meet ThreadPoolExecutor
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- "Xiaodeng" domain password policy enhancer in operation and maintenance
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- 快解析——好用的内网安全软件
- 如何将自己的代码作品快速存证,已更好的保护自己劳动成果
- [paper reading] cavemix: a simple data augmentation method for brain vision segmentation
- Design of emergency lighting evacuation indication system for urban rail transit station
猜你喜欢

Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC

巩固表达式C# 案例简单变量运算

C language to quickly solve the reverse linked list

电力运维云平台:开启电力系统“无人值班、少人值守”新模式

The caching feature of docker image and dockerfile

企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)

How long does it take to obtain a PMP certificate?
![[path planning] RRT adds dynamic model for trajectory planning](/img/98/dd9b106fd9dc64e676d9c943c03ab3.jpg)
[path planning] RRT adds dynamic model for trajectory planning

IELTS examination process, what to pay attention to and how to review?

ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
随机推荐
How long does it take to obtain a PMP certificate?
It's too convenient. You can complete the code release and approval by nailing it!
Significance of acrel EMS integrated energy efficiency platform in campus construction
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
实战模拟│JWT 登录认证
The pit of sizeof operator in C language
「运维有小邓」域密码策略强化器
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
他做国外LEAD,用了一年时间,把所有房贷都还清了
Using the uniapp rich text editor
城市轨道交通站应急照明疏散指示系统设计
Introduction to ACM combination counting
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
Consolidated expression C case simple variable operation
22-07-02周总结
【雅思阅读】王希伟阅读P4(matching1)
Using fast parsing intranet penetration to realize zero cost self built website
香港珠宝大亨,22亿“抄底”佐丹奴
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
[Peking University] tensorflow2.0-1-opening