当前位置:网站首页>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)
}
}

边栏推荐
- 雅思考试流程、需要具体注意些什么、怎么复习?
- Design of emergency lighting evacuation indication system for urban rail transit station
- Advanced template
- Five papers recommended for the new development of convolutional neural network in deep learning
- 基本放大电路的学习
- How to use fast parsing to make IOT cloud platform
- The pit of sizeof operator in C language
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
- Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
- Microservice
猜你喜欢
随机推荐
Upload avatar on uniapp
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
Is the account opening link of Huatai Securities with low commission safe?
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
How to use fast parsing to make IOT cloud platform
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
使用快解析搭建自己的minecraft服务器
[IELTS reading] Wang Xiwei reading P4 (matching1)
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
认识ThreadPoolExecutor
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
人脸识别5- insight-face-paddle-代码实战笔记
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
【雅思阅读】王希伟阅读P4(matching1)
JS how to realize array to tree
ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning