当前位置:网站首页>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)
}
}
边栏推荐
- Hologres Query管理及超时处理
- C语言中sizeof操作符的坑
- js正则表达式之中文验证(转)
- Netcore3.1 JSON web token Middleware
- Is it safe to open and register new bonds? Is there any risk? Is it reliable?
- uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
- PMP证书续证流程
- 快解析——好用的内网安全软件
- PMP certificate renewal process
- S32 design studio for arm 2.2 quick start
猜你喜欢
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
XML的解析
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
45岁教授,她投出2个超级独角兽
Application of multi loop instrument in base station "switching to direct"
OpenHarmony资源管理详解
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
How to use fast parsing to make IOT cloud platform
Using the uniapp rich text editor
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
随机推荐
PaddleOCR教程
积分商城游戏设置的基本要点
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
如何有效对直流列头柜进行监测
[IELTS reading] Wang Xiwei reading P4 (matching1)
Combien de temps faut - il pour obtenir un certificat PMP?
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Using the uniapp rich text editor
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
Fast analysis -- easy to use intranet security software
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
Netcore3.1 JSON web token Middleware
It's too convenient. You can complete the code release and approval by nailing it!
js正则表达式之中文验证(转)
Application of multi loop instrument in base station "switching to direct"
Summer challenge brings you to play harmoniyos multi terminal piano performance
同事的接口文档我每次看着就头大,毛病多多。。。
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
P4281 [AHOI2008]紧急集合 / 聚会(LCA)