当前位置:网站首页>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)
}
}
边栏推荐
- Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- uniapp 除了数字,其他输入无效
- S32 design studio for arm 2.2 quick start
- 【监控】zabbix
- After Microsoft disables the IE browser, open the IE browser to flash back the solution
- [JS] - [sort related] - Notes
- Consolidated expression C case simple variable operation
- C语言中sizeof操作符的坑
- Business implementation - the log is written to the same row of data
猜你喜欢
45 year old professor, she threw two super unicorns
Design of emergency lighting evacuation indication system for urban rail transit station
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
Selected cutting-edge technical articles of Bi Ren Academy of science and technology
45岁教授,她投出2个超级独角兽
[JS] - [sort related] - Notes
Summer challenge brings you to play harmoniyos multi terminal piano performance
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
人脸识别5- insight-face-paddle-代码实战笔记
随机推荐
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
Parsing of XML
基本放大电路的学习
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
模板的进阶
PMP certificate renewal process
[monitoring] ZABBIX
Introduction to ACM combination counting
企业应用业务场景,功能添加和修改C#源码
基于三维gis平台的消防系统运用
How to effectively monitor the DC column head cabinet
Expand your kubecl function
Skills in analyzing the trend chart of London Silver
45岁教授,她投出2个超级独角兽
Is it safe to open and register new bonds? Is there any risk? Is it reliable?
Application of multi loop instrument in base station "switching to direct"
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
How to save your code works quickly to better protect your labor achievements
认识ThreadPoolExecutor