8.鸿蒙app java代码构建用户界面

鸿蒙app java代码构建用户界面

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="#8F71A6F5"/>
</shape>

MainAbilitySlice.java

复制代码
package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel loglabel2 = new HiLogLabel(HiLog.LOG_APP,0x001101,"MainAbility");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//       super.setUIContent(ResourceTable.Layout_ability_main);
//        HiLog.info(loglabel2,"onStart-----------------------------------");
//        int ppi = getContext().getResourceManager().getDeviceCapability().screenDensity;
//
//        HiLog.info(loglabel2,"屏幕密度:"+Integer.toString(ppi));
//
//      //  Text text2 = new Text();
//        DirectionalLayout.LayoutConfig configForText = new DirectionalLayout.LayoutConfig(
//                toPixels(100,getContext()),
//                toPixels(16,getContext())
//        );
//        //text2.setLayoutConfig(configForText);//如果报错,则导入import ohos.agp.components.Text;
//       // text2.set
//        Text text1 = (Text) findComponentById(ResourceTable.Id_text_1);
//        Text text2 = (Text) findComponentById(ResourceTable.Id_text_2);
//        Text text3 = (Text) findComponentById(ResourceTable.Id_text_3);
//        Text text3a = (Text) findComponentById(ResourceTable.Id_text_3a);
//        text1.setText("美好时光1");
//        text2.setText("万事如意");
//        text3.setText("好运常在");
//        text3a.setText("鸿蒙app demo");
//
//        Button button = (Button) findComponentById(ResourceTable.Id_btn1);
//        button.setText("启动");
//        button.setClickedListener(new Component.ClickedListener() {
//            @Override
//            public void onClick(Component component) {
//                new ToastDialog(getContext())
//                        .setText("设备已经启动。。。。。")
//                        .setAlignment(LayoutAlignment.CENTER)
//                        .show();
//            }
//        });
        ComponentContainer.LayoutConfig configForLayout = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_PARENT
                ); //设置宽与高,match_parent
        DirectionalLayout layout = new DirectionalLayout(this);
        layout.setLayoutConfig(configForLayout);
        layout.setOrientation(DirectionalLayout.VERTICAL);

        DirectionalLayout.LayoutConfig configForText= new DirectionalLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT

                );
        configForText.alignment = LayoutAlignment.HORIZONTAL_CENTER;
        Text text = new Text(this);
        text.setLayoutConfig(configForText);
        text.setBackground(new ShapeElement(
                getContext(),
                ResourceTable.Graphic_background_ability_main
        ));
        text.setText("hello world by java");
        text.setTextSize(50);
        layout.addComponent(text);
        //讲UI内容设置为定向布局
        super.setUIContent(layout);

    }
    /**
     *虚拟像素转物理像素
     */
    private  static int toPixels(int value, Context context)
    {
        return value * context.getResourceManager().getDeviceCapability().screenDensity/160;
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
相关推荐
Bug退退退12328 分钟前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
小皮侠29 分钟前
nginx的使用
java·运维·服务器·前端·git·nginx·github
Zz_waiting.41 分钟前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象
全栈凯哥41 分钟前
02.SpringBoot常用Utils工具类详解
java·spring boot·后端
兮动人1 小时前
获取终端外网IP地址
java·网络·网络协议·tcp/ip·获取终端外网ip地址
呆呆的小鳄鱼1 小时前
cin,cin.get()等异同点[面试题系列]
java·算法·面试
独立开阀者_FwtCoder1 小时前
"页面白屏了?别慌!前端工程师必备的排查技巧和面试攻略"
java·前端·javascript
Touper.1 小时前
JavaSE -- 泛型详细介绍
java·开发语言·算法
静若繁花_jingjing1 小时前
Redis线程模型
java·数据库·redis
塞尔维亚大汉2 小时前
鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗
源码·harmonyos