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);
    }
}
相关推荐
ybq1951334543114 分钟前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339461 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8501 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT2 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟3 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode4 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社24 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理4 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码4 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot
YuTaoShao5 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展