当前位置:网站首页>Detailed explanation of openharmony resource management
Detailed explanation of openharmony resource management
2022-07-05 00:06:00 【InfoQ】
Resource classification
resources
resoures
base
rawfile

base
element
media
animation
layout
graphic
profile

Resource group Directory
List of determiners
- When matching the corresponding resource file for the device , The priority of qualifier directory matching is from high to low : Mobile country code and mobile network code > Area ( Optional combination : Language 、 Language _ written words 、 Language _ Country or region 、 Language _ written words _ Country or region )> Horizontal and vertical screen > Device type > Color mode > Screen density .
- If the qualifier directory containsMobile country code and mobile network code 、 Language 、 written words 、 Horizontal and vertical screen 、 Device type 、 Color modequalifier , The value of the corresponding qualifier must be consistent with the current equipment status , This directory can participate in resource matching of devices . for example , List of determiners “zh_CN-car-ldpi” Can't participate in “en_US” Resource matching of devices .
Access to resources
- Access application resourcesCatalog
base
The resource files in the directory will be compiled into binary files and given unique ID , When using the corresponding resources, use the resource accessor$('app.type.name')In the form of ,appRepresents in applicationresources
Resources defined in the directory ;typeRepresents the resource type , There arecolor
、float
、string
、string
、media
etc. ;nameThe name of the file that represents the resource . for examplemediaChina, Singapore and Canadanameby Car.svg Pictures of the , Then access the string resource as$r('app.media.Car').
- The author is in
base
New under the directorystring.json
andcolor.json
file , Store string and color respectively , The resource content is shown in the figure below :

- adopt$('app.type.name')A simple example of accessing resources is shown below :
@Entry
@Component
struct Index {
build() {
Row() {
Column() {
Text($r('app.string.title_desc')) // Access string resources
.fontSize(60).fontWeight(FontWeight.Bold)
.fontColor($r('app.color.title_color')) // Access font colors
.backgroundImage($r('app.media.Car')) // Device background picture
Image("common/images/Car.svg").objectFit(ImageFit.Contain).height(200)
}
.width('100%')
}
.height('100%')
}
}

- Access system resources
- System resources include
Color
、Round corners
、typeface
、spacing
、character string
Andpicture
etc. , By using system resources , Different developers can develop applications with the same visual style , Developers can use$r('sys.type.name')Reference system resources in the form of , Unlike accessing application resources, usesysRepresents system resources , Others are consistent with the rules for accessing application resources .
- A simple example of accessing system resources is shown below :
@Entry
@Component
struct ResourceTest {
build() {
Column() {
Text($r('app.string.title_desc')) //** Access the application resource directory **
.fontColor($r('sys.color.ohos_fa_alert')) //** Access the system resource directory **
.fontSize($r('sys.float.ohos_id_text_size_headline3'))
.backgroundColor($r('sys.color.ohos_id_color_palette_aux1'))
Image("/common/images/Car.svg") //** Create your own directory **
.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") //** Create your own directory **
.objectFit(ImageFit.None)
.border({
color: Color.Orange,
radius: 20,
width: 12
})
.margin({
top: 50,
})
.width(200)
.height(200)

Explorer
@ohos.resourceManager
ResourceManager
resourceManager
declare namespace resourceManager {
// obtain ResourceManager
export function getResourceManager(callback: AsyncCallback<ResourceManager>): void;
// Get specified bundleName Of ResourceManager
export function getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void;
export interface ResourceManager {
// Get string resources
getString(resId: number, callback: AsyncCallback<string>): void;
// Get string array resource
getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void;
// Get media resources
getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void;
// Get device information , For example, the current screen density , Whether the device type is mobile phone or tablet
getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void;
// Get configuration information , For example, the current screen direction density , Current device language
getConfiguration(callback: AsyncCallback<Configuration>): void;
// Release ResourceManager resources
release();
}
}
export default resourceManager;
ResourceManager
getResourceManager()
ResourceManager
getXXX()
ResourceManager
- introduce resourceManager
import resourceManager from '@ohos.resourceManager';
- obtain ResourceManager
aboutToAppear() {
resourceManager.getResourceManager((error, manager) => {
// obtain manager
})
}
- Use ResourceManager
manager.getString(0x1000001, (innerError, data) => {
if(data) {
// Resource acquisition success
} else {
console.log("error: " + JSON.stringify(innerError))
}
})
import resourceManager from '@ohos.resourceManager';
@Entry @Component struct ResourceTest {
@State text_string: string = " Learn from nuts ";
@State capability: string = "OpenHarmony";
@State configuration: string = " application development ";
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) // Access string resources
// Set dimensions
.fontSize(29)
.fontColor($r('app.color.title_color')) // Access font colors
Text(this.capability) // capability Information
.fontSize(40).fontWeight(FontWeight.Bold)
Text(this.configuration) // configuration Information
.fontSize(60)
}
.width('100%')
.height('100%')
.padding(10)
}
}

边栏推荐
- js正则表达式之中文验证(转)
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- 45 year old professor, she threw two super unicorns
- PMP certificate renewal process
- [paper reading] Tun det: a novel network for meridian ultra sound nodule detection
- js如何实现数组转树
- 基本放大电路的学习
- 【路径规划】RRT增加动力模型进行轨迹规划
- 如何有效对直流列头柜进行监测
- 机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
猜你喜欢
[JS] - [sort related] - Notes
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
Date time type and format in MySQL
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
微服务(Microservice)那点事儿
[path planning] RRT adds dynamic model for trajectory planning
圖解網絡:什麼是網關負載均衡協議GLBP?
同事的接口文档我每次看着就头大,毛病多多。。。
MIT-6.824-lab4B-2022(万字思路讲解-代码构建)
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
随机推荐
How many triangles are there in the golden K-line diagram?
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
Using fast parsing intranet penetration to realize zero cost self built website
人脸识别5- insight-face-paddle-代码实战笔记
[Peking University] tensorflow2.0-1-opening
企业应用业务场景,功能添加和修改C#源码
How to do the project of computer remote company in foreign Internet?
【北京大学】Tensorflow2.0-1-开篇
PaddleOCR教程
XML的解析
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
同事的接口文档我每次看着就头大,毛病多多。。。
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
人生无常,大肠包小肠, 这次真的可以回家看媳妇去了。。。
OSEK standard ISO_ 17356 summary introduction
雅思考试流程、需要具体注意些什么、怎么复习?
多回路仪表在基站“转改直”方面的应用
Basic points of the game setup of the points mall
电力运维云平台:开启电力系统“无人值班、少人值守”新模式