鸿蒙java项目目录解读,及资源使用
鸿蒙项目结构项目名|---.gradle //gradle 的生成文件,不要动它|---entry //应用的主模块||---build //构建项目自动生成||---libs //第三方jar||---src //源码|||---main //源码||||---java//源码||||---resources //资源https://developer.harmonyos.com/cn/docs
·
鸿蒙项目结构,及资源使用
项目名
|---.gradle //gradle 的生成文件,不要动它
|---entry //应用的主模块
| |---build //构建项目自动生成
| |---libs //第三方jar
| |---src //源码
| | |---main //源码
| | | |---java//源码
| | | |---resources //资源https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-resource-file-categories-0000001052066099
| | | | |---base // 默认存在的目录,它与限定词目录中的文件大部分会编译成二进制文件并赋予id 可以通过ResourcesTable引用
| | | | | |---element //表示元素资源(子文件只能有一个,不能重复)
| | | | | | |---boolean.json //布尔型
| | | | | | |---color.json //颜色
| | | | | | |---float.json //浮点型
| | | | | | |---intarray.json //整型数组
| | | | | | |---integer.json //整型
| | | | | | |---pattern.json //样式
| | | | | | |---plural.json //复数形式
| | | | | | |---strarray.json //字符串数组
| | | | | | |---string.json //字符串
| | | | | |---media //表示媒体资源 图片 视频等都在这里
| | | | | | |---icon.png//项目创建时候的默认app 图标
| | | | | |---animation //表示动画资源xml
| | | | | |---layout //表示布局资源xml,如果用xml 写页面 页面就在这里
| | | | | |---graphic//表示可绘制资源xml,比如按钮
| | | | | |---profile//表示其他类型文件(编译保存原始文件,其他都转二进制保存)
| | | | |---en_GB-vertical-car-mdpi
/*限定词目录示例,需要开发者自行创建,这里如果定义了限定的设备或应用那么就应用这里的,如果这里没有才用base的,这个不是必须都有,可以只有一个限定词,但是顺序不能错.
它的格式为:语言_文字_国家或地区-横竖屏-设备类型-屏幕密度 ,语言、文字、国家或地区之间采用下划线(_)连接,除此之外的其他限定词之间均采用中划线(-)连接。例如:zh_Hant_CN、zh_CN-car-ldpi。
zh表示中文,en表示英语;详细取值范围,参见ISO 639-1(ISO制定的语言编码标准)。
Hans表示简体中文,Hant表示繁体中文。详细取值范围,参见ISO 15924(ISO制定的文字编码标准)。
CN表示中国,GB表示英国。详细取值范围,参见ISO 3166-1(ISO制定的国家和地区编码标准)。
vertical:竖屏 horizontal:横屏;
car:车机tv:智慧屏wearable:智能穿戴;
屏幕密度从低到高:sdpi 0-120 mdpi 120-160 ldpi 160-240 xldpi 240-320 xxldpi 320-480 xxxldpi 480-640
dpi(分辨率/屏幕尺寸) 1vp=160dpi屏幕上的1px
*/
| | | |---config.json //配置文件https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-config-file-overview-0000000000011951
| | | | |---rawfile // 默认存在的目录 编译时直接打包 不可以通过ResourcesTable引用 文件夹和文件名都是自己定
| | |---test //测试
| |---build.gradle //gradle配置文件 可在这里引入第三方JAR包 这里常用 比外层的常用
|---gradle //项目构建工具
|---build.gradle //gradle配置文件 可在这里引入第三方JAR包
|---gradlew //gradle命令脚本不常用 linux 用
|---gradlew.bat //gradle命令脚本不常用 window 用
|---local.properties //鸿蒙本地配置文件 sdk和node 的引入地址在这里修改
|---settings.gradle //gradle全局配置
External Libraries //第三方jar 包
资源文件的使用官网有例子 链接:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-resource-file-example-0000001051733014
这里只记录没有例子的情况:
1、profile 保存的是一个名字是test1的文本
在Java文件中,获取profile中的文件内容。
这里profile 保存的是一个名字是test1的文本
获取资源用getResourceManager().getResource(ResourceTable.Profile_test1);
Resource resource = null;
try {
Text text = (Text) findComponentById(ResourceTable.Id_text);
//获取资源
resource = getResourceManager().getResource(ResourceTable.Profile_test1);
//转化资源
InputStreamReader inputStreamReader = new InputStreamReader(resource, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineTxt = "";
//把资源内容写道页面上面
while((lineTxt = bufferedReader.readLine()) != null){
text.append(","+lineText);
}
} catch (Exception e) {
}
2、获取系统资源
对于系统文件的xml 获取,格式是$ohos:type:name。目前官方开发的资源只有3个也就是ic_app,request_location_reminder_title和request_location_reminder_content 后面两个都是字符串使用方法如下:
<Text ohos:text="$ohos:string:request_location_reminder_title"/>
ic_app目前没有找到方法 不过这个图片就是media文件夹下的icon可以这样引用
<Image
ohos:id="$+id:image2"
ohos:width="match_content"
ohos:height="match_content"
ohos:layout_alignment="horizontal_center"
ohos:image_src="$media:icon"/>
3、rawfile里面读取一个文本
Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
try {
Resource resource = getResourceManager().getRawFileEntry("resources/rawfile/test.text").openRawFile();//这一行和官方例子有点不一样 请注意
InputStreamReader inputStreamReader = new InputStreamReader(resource, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str;
while ((str=bufferedReader.readLine())!=null){
text.append(","+str);
}
} catch (IOException e) {
e.printStackTrace();
}
更多推荐
所有评论(0)