用到的软件是 DevEco Studio

首先第一步新建项目File>New Porject
项目名字MyAppLication
Device我选择的是TVTemplate我选的是java

打开src>main>resources>base>layout
没有文件就新建一个
在layout下新建ability_main.xml,内容如下

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:width="match_parent"
    ohos:height="match_parent"
    ohos:background_element="#000000">
    <Text
        ohos:id="$+id:text"
        ohos:width="match_content"
        ohos:height="match_content"
        ohos:center_in_parent="true"
        ohos:text="甘霖娘"
        ohos:text_color="white"
        ohos:text_size="32fp"/>
    <Button
        ohos:id="$+id:button"
        ohos:width="match_content"
        ohos:height="match_content"
        ohos:text_size="19fp"
        ohos:text="Next"
        ohos:top_padding="8vp"
        ohos:bottom_padding="8vp"
        ohos:right_padding="80vp"
        ohos:left_padding="80vp"
        ohos:text_color="white"
        ohos:background_element="$graphic:button_element"
        ohos:center_in_parent="true"
        ohos:align_parent_bottom="true"/>
</DependentLayout>

打开src>main>resources>base>graphic
没有文件就新建一个
在graphic下新建background_ability_main.xml,内容

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <solid
        ohos:color="#007DFF"/>
</shape>

打开src>main>java>com>example>myapplication>slice
MainAbilitySlice.java,写入代码

public class MainAbilitySlice extends AbilitySlice {
    //声明布局
    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //布局宽高
        AdaptiveBoxLayout.LayoutConfig config = new AdaptiveBoxLayout.LayoutConfig(AdaptiveBoxLayout.LayoutConfig.MATCH_PARENT, AdaptiveBoxLayout.LayoutConfig.MATCH_PARENT);
        //设置布局属性
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        //设置页面背景颜色
        myLayout.setBackground(element);
        //声明Text组件
        Text text = new Text(this);
        //为组件添加对应布局的布局属性
        text.setLayoutConfig(config);
        //显示内容
        text.setText("甘霖娘");
        //文字颜色
        text.setTextColor(new Color(0xFF000000));
        //文字大小
        text.setTextSize(50);
        //居中
        text.setTextAlignment(TextAlignment.CENTER);
        //将组件添加到布局中
        myLayout.addComponent(text);
        //将布局作为根布局添加到视图树中
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

以上写的很多非常不美观,改为加载xml布局,修改如下

public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);//加载xml布局
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

接下来在src>main>java>com>example>myapplication>slice下面新建
SecondAbilitySlice.java 写入代码

public class SecondAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        /*  使用代码设置布局  */

        //声明一个相关布局
        DependentLayout myLayout = new DependentLayout(this);
        //设置布局的大小 宽高占满屏幕
        myLayout.setWidth(MATCH_PARENT);
        myLayout.setHeight(MATCH_PARENT);
        //创建一个形状元素对象
        ShapeElement element = new ShapeElement();
        //设置颜色
        element.setRgbColor(new RgbColor(0,0,0));
        //设置布局的背景
        myLayout.setBackground(element);

        //创建一个文本组件
        Text text = new Text(this);
        //设置文本组件显示的内容
        text.setText("鸡掰");
        //设置宽度占满父布局
        text.setWidth(MATCH_PARENT);
        //设置文字大小
        text.setTextSize(55);
        //设置文字颜色
        text.setTextColor(Color.WHITE);
        //设置文本的布局 宽高自适应
        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(MATCH_CONTENT,MATCH_CONTENT);
        //添加显示规则 显示在布局的中央
        textConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
        //给Text组件设置布局配置
        text.setLayoutConfig(textConfig);
        //然后把Text组件添加到布局中
        myLayout.addComponent(text);
        //最后加载布局
        super.setUIContent(myLayout);
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

回到MainAbilitySlice.java 添加 onStart方法代码

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //初始化组件
        Button button = (Button) findComponentById(ResourceTable.Id_text_helloworld);
        if(button != null){
            //设置点击监听
            button.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    // 还是通过Intent来跳转
                    Intent secondIntent = new Intent();
                    // 指定待启动FA的bundleName和abilityName
                    Operation operation = new Intent.OperationBuilder()
                            // 设备id
                            .withDeviceId("ggg")
                            // 应用的包名
                            .withBundleName("com.example.myapplication")
                            // 跳转目标的路径名 通常是包名+类名 或者 . + 类名
                            .withAbilityName("com.example.myapplication.SecondAbilitySlice")
                            //.withAbilityName(".SecondAbilitySlice")
                            .build();
                    // 设置操作方式
                    secondIntent.setOperation(operation);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(secondIntent);
                }
            });
        }
    }

然后就可以运行了
在最上方一排,Tools里选择HVD Manager
会跳转到网页,需要注册才能使用,注册完登陆后回到软件
添加一个虚拟器选择 TV 或 手表
建议选择手表
就可以了
因为懒得截图就没有图片,希望你不要不识好歹

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐