From 2ce888884f62c182636fe19422e090c1ef81e34b Mon Sep 17 00:00:00 2001 From: gitee-bot Date: Sun, 5 Apr 2026 02:11:22 +0000 Subject: [PATCH] Update README.md --- README.md | 310 +----------------------------------------------------- 1 file changed, 4 insertions(+), 306 deletions(-) diff --git a/README.md b/README.md index 00305c0..f74974e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ + + # XmlParser SDK [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) @@ -66,7 +68,7 @@ XmlParser SDK是一个轻量级的Android XML布局解析库,旨在提供灵 ## 架构设计 ``` -xmlparser-sdk/ +XmlParser SDK/ ├── core/ # 核心模块 │ ├── model/ # 数据模型 │ │ └── ViewNode.java # 视图节点数据结构 @@ -208,308 +210,4 @@ setContentView(view); ViewNode rootNode = new ViewNode(); rootNode.setViewName("LinearLayout"); -HashMap attributes = new HashMap<>(); -attributes.put("android:orientation", "vertical"); -attributes.put("android:layout_width", "match_parent"); -attributes.put("android:layout_height", "match_parent"); -rootNode.setAttributes(attributes); - -// 创建视图 -XmlParserSDK sdk = new XmlParserSDK(); -View view = sdk.createView(rootNode, this); -setContentView(view); -``` - -### 高级使用 - -#### 1. 自定义属性处理器 - -```java -// 创建自定义属性解析器 -DefaultAttributeParser attributeParser = new DefaultAttributeParser(); - -// 注册自定义属性处理器 -attributeParser.registerHandler("app:customShadow", (view, value) -> { - // 解析自定义属性值 - float shadowRadius = Float.parseFloat(value); - // 应用阴影效果 - view.setElevation(shadowRadius); -}); - -// 使用自定义属性解析器 -XmlParserSDK sdk = new XmlParserSDK(attributeParser); -``` - -#### 2. 自定义视图创建器 - -```java -// 创建自定义视图创建器 -public class CustomButtonCreator extends BaseViewCreator { - @Override - public CustomButton createView(Context context) { - return new CustomButton(context); - } - - @Override - public void applyAttributes(CustomButton view, Map attributes) { - super.applyAttributes(view, attributes); - - // 处理自定义属性 - if (attributes.containsKey("app:customText")) { - view.setCustomText(attributes.get("app:customText")); - } - } -} - -// 注册自定义视图创建器 -XmlParserSDK sdk = new XmlParserSDK(); -sdk.getViewFactory().registerCreator("CustomButton", new CustomButtonCreator()); -``` - -#### 3. 批量注册属性处理器 - -```java -DefaultAttributeParser attributeParser = new DefaultAttributeParser(); - -// 批量注册属性处理器 -Map handlers = new HashMap<>(); -handlers.put("app:roundedCorners", this::handleRoundedCorners); -handlers.put("app:borderWidth", this::handleBorderWidth); -handlers.put("app:borderColor", this::handleBorderColor); - -for (Map.Entry entry : handlers.entrySet()) { - attributeParser.registerHandler(entry.getKey(), entry.getValue()); -} -``` - -## 高级配置 - -### SDK配置选项 - -```java -// 创建SDK配置 -XmlParserSDK.Config config = new XmlParserSDK.Config.Builder() - .setEnableCache(true) // 启用视图缓存 - .setCacheSize(100) // 设置缓存大小 - .setStrictMode(false) // 关闭严格模式 - .setLogEnabled(true) // 启用日志 - .build(); - -// 使用配置创建SDK -XmlParserSDK sdk = new XmlParserSDK(config); -``` - -### ProGuard配置 - -如果使用ProGuard,需要添加以下配置: - -```proguard -# 保留XmlParser SDK相关类 --keep class com.focustech.xmlparser.** { *; } - -# 保留自定义属性处理器 --keep class * implements com.focustech.xmlparser.view.attribute.AttributeHandler { *; } - -# 保留自定义视图创建器 --keep class * implements com.focustech.xmlparser.view.creator.IViewCreator { *; } -``` - -## 支持的属性 - -### 通用属性(适用于所有View) - -| 属性名 | 说明 | 示例值 | -|--------|------|--------| -| android:layout_width | 宽度 | match_parent, wrap_content, 100dp | -| android:layout_height | 高度 | match_parent, wrap_content, 100dp | -| android:layout_margin | 外边距 | 16dp | -| android:layout_marginLeft | 左外边距 | 8dp | -| android:layout_marginRight | 右外边距 | 8dp | -| android:layout_marginTop | 上外边距 | 8dp | -| android:layout_marginBottom | 下外边距 | 8dp | -| android:padding | 内边距 | 16dp | -| android:paddingLeft | 左内边距 | 8dp | -| android:paddingRight | 右内边距 | 8dp | -| android:paddingTop | 上内边距 | 8dp | -| android:paddingBottom | 下内边距 | 8dp | -| android:id | 视图ID | @+id/my_view | -| android:background | 背景 | #FFFFFF, @color/white | -| android:clickable | 是否可点击 | true, false | - -### TextView属性 - -| 属性名 | 说明 | 示例值 | -|--------|------|--------| -| android:text | 文本内容 | Hello World | -| android:textSize | 文本大小 | 16sp | -| android:textColor | 文本颜色 | #000000, @color/black | -| android:gravity | 文本对齐 | center, left, right | - -### EditText属性 - -| 属性名 | 说明 | 示例值 | -|--------|------|--------| -| android:hint | 提示文本 | Please input | -| android:inputType | 输入类型 | text, number, phone | - -### ImageView属性 - -| 属性名 | 说明 | 示例值 | -|--------|------|--------| -| android:src | 图片资源 | @drawable/image | -| android:scaleType | 缩放类型 | centerCrop, fitXY | - -### LinearLayout属性 - -| 属性名 | 说明 | 示例值 | -|--------|------|--------| -| android:orientation | 排列方向 | vertical, horizontal | - -## 性能优化 - -### 1. 使用编译时生成模式 - -对于静态布局,推荐使用编译时生成模式: - -```java -// 使用生成的类 -View rootView = FastInflate_activity_mainxml.getRootView(this); -setContentView(rootView); -``` - -### 2. 启用视图缓存 - -```java -XmlParserSDK.Config config = new XmlParserSDK.Config.Builder() - .setEnableCache(true) - .setCacheSize(100) - .build(); -``` - -### 3. 复用SDK实例 - -```java -// 推荐:复用SDK实例 -private XmlParserSDK sdk; - -@Override -protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - sdk = new XmlParserSDK(); - - // 多次使用同一个SDK实例 - View view1 = sdk.parse(inputStream1, this); - View view2 = sdk.parse(inputStream2, this); -} -``` - -### 4. 异步加载 - -对于大型布局,建议使用异步加载: - -```java -new AsyncTask() { - @Override - protected View doInBackground(Void... voids) { - XmlParserSDK sdk = new XmlParserSDK(); - return sdk.parse(getAssets().open("large_layout.xml"), MainActivity.this); - } - - @Override - protected void onPostExecute(View view) { - setContentView(view); - } -}.execute(); -``` - -## 注意事项 - -### 1. 线程安全 - -- SDK实例不是线程安全的,不要在多线程中共享同一个实例 -- 视图创建必须在主线程中进行 - -### 2. 内存管理 - -- 及时释放不再使用的ViewNode对象 -- 避免在循环中频繁创建SDK实例 - -### 3. 异常处理 - -- 始终捕获ParseException和ViewCreateException -- 在异常处理中提供用户友好的错误提示 - -### 4. 兼容性 - -- 最低支持Android 5.0 (API 21) -- 推荐使用AndroidX库 - -### 5. 性能建议 - -- 对于频繁使用的布局,使用编译时生成模式 -- 对于动态布局,使用运行时解析模式 -- 合理设置缓存大小 - -## 更新日志 - -### v1.0.0 (2026-04-04) - -- 初始版本发布 -- 支持运行时XML解析 -- 支持编译时代码生成 -- 支持自定义视图和属性处理器 -- 提供向后兼容层 - -## 贡献指南 - -我们欢迎所有形式的贡献,包括但不限于: - -- 提交Bug报告 -- 提交功能请求 -- 提交代码改进 -- 完善文档 - -### 提交代码 - -1. Fork本仓库 -2. 创建特性分支 (`git checkout -b feature/AmazingFeature`) -3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) -4. 推送到分支 (`git push origin feature/AmazingFeature`) -5. 创建Pull Request - -### 代码规范 - -- 遵循Java代码规范 -- 添加必要的注释 -- 编写单元测试 -- 更新相关文档 - -## 许可证 - -本项目采用Apache 2.0许可证,详见[LICENSE](LICENSE)文件。 - -``` -Copyright 2026 FocusTech - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -``` - -## 联系方式 - -- 项目主页:[https://github.com/your-repo/XmlToView](https://github.com/your-repo/XmlToView) -- 问题反馈:[https://github.com/your-repo/XmlToView/issues](https://github.com/your-repo/XmlToView/issues) -- 邮箱:support@focustech.com - -## 致谢 - -感谢所有为本项目做出贡献的开发者! \ No newline at end of file +HashMap \ No newline at end of file -- Gitee