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);
    }
}
相关推荐
进阶的架构师1 分钟前
互联网Java工程师面试题及答案整理(2024年最新版)
java·开发语言
黄俊懿1 分钟前
【深入理解SpringCloud微服务】手写实现各种限流算法——固定时间窗、滑动时间窗、令牌桶算法、漏桶算法
java·后端·算法·spring cloud·微服务·架构
木子020410 分钟前
java高并发场景RabbitMQ的使用
java·开发语言
夜雨翦春韭21 分钟前
【代码随想录Day29】贪心算法Part03
java·数据结构·算法·leetcode·贪心算法
大霞上仙44 分钟前
jmeter学习(1)线程组与发送请求
java·学习·jmeter
笃励1 小时前
Java面试题二
java·开发语言·python
Android技术栈1 小时前
鸿蒙开发(NEXT/API 12)【管理应用与Wear Engine服务的连接状态】手机侧应用开发
服务器·harmonyos·鸿蒙·鸿蒙系统·openharmony
易雪寒1 小时前
IDEA在git提交时添加忽略文件
java·git·intellij-idea
golitter.2 小时前
Vue组件库Element-ui
前端·vue.js·ui
打码人的日常分享2 小时前
企业人力资源管理,人事档案管理,绩效考核,五险一金,招聘培训,薪酬管理一体化管理系统(源码)
java·数据库·python·需求分析·规格说明书