目录

开发方法

创建工程

使用HUAWEI DevEco Studio创建新工程。细节可参见HUAWEI DevEco Studio使用指南。

  1. 新建项目,选择“File > New > Project”。

  2. 填写项目名称和包名。

  3. 等待工程编译成功。

华为UI Engine控件使用方法

华为UI Engine控件有三种基本使用方法,下面以UI界面线性布局下HwButton的使用为例做介绍。

[h2]HUAWEI DevEco Studio可视化控件拖拽编辑

选中布局xml文件,选择相应Design界面。左侧标签下选择Huawei子标签,点击选中需要使用的控件,将其拖动到layout布局上,然后可以在右侧Attributes标签下设置当前选中控件的基本属性参数。

拖拽后HwButton代码会在xml文件Text界面的相应布局中自动生成。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/parent"
    android:gravity="center"
    android:orientation="vertical">
    <com.huawei.uikit.hwbutton.widget.HwButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="HwButton"
        android:id="@+id/hwButton"/>
</LinearLayout>

可视化控件拖拽编辑方式会自动添加相应控件的依赖,无需配置。

[h2]编辑xml文件

使用前需在build.gradle的dependencies中添加所引用控件的依赖:

dependencies {
    implementation 'com.huawei.ui.uikit:hwbutton-phone:{version}'
}

{version}需替换为实际的版本号,具体版本号请参见版本更新说明章节。例如:implementation 'com.huawei.ui.uikit:hwbutton-phone:1.0.0.500'。

在xml界面中直接添加HwButton的相关描述:

<com.huawei.uikit.hwbutton.widget.HwButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HwButton Demo" />

[h2]动态创建

本方法可以在逻辑代码中直接生成相关的控件。
  1. 使用前需在build.gradle的dependencies中添加所引用控件的依赖。
    dependencies {
        implementation 'com.huawei.ui.uikit:hwbutton-phone:{version}'
    }

    {version}需替换为实际的版本号,具体版本号请参见版本更新说明章节。例如:implementation 'com.huawei.ui.uikit:hwbutton-phone:1.0.0.500'。

  2. 导入控件包。
    import com.huawei.uikit.hwbutton.widget.HwButton;
  3. 在代码中创建HwButton。
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout parent = findViewById(R.id.parent);
        HwButton hwButton = HwButton.instantiate(this);
        if (hwButton != null) {
            hwButton.setText("Code instantiate HwButton");
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
            parent.addView(hwButton, params);
        }
    }

    根据所选布局(本示例为线性布局)修改相关参数。

效果预览

在xml文件界面下,点击右侧Preview按钮可以查看华为UI Engine控件的显示效果。

华为UI Engine多设备开发

华为UI Engine提供能适应多种设备显示效果的多态控件。

[h2]HUAWEI HiCar应用UI Engine开发

华为UI Engine为HUAWEI HiCar应用提供相应控件的车机效果。HUAWEI HiCar应用开发请参见开发者联盟网站HUAWEI HiCar开发指南相关描述。

在创建好的HUAWEI HiCar应用工程下的res目录下会自动生成layout-car文件夹,在layout、layout-car中分别开发应用的手机和车机相关界面。

HUAWEI HiCar应用的华为UI Engine使用方法与前文无异。开发完成后,HUAWEI HiCar应用会根据显示设备自动切换显示效果。