鸿蒙os ui,鸿蒙OS应用开发之——Java UI框架-组件与布局开发

public class ExampleAbilitySlice extends AbilitySlice {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

// 声明布局

DirectionalLayout directionalLayout = new DirectionalLayout(getContext());

// 设置布局大小

directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);

directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);

// 设置布局属性

directionalLayout.setOrientation(Component.VERTICAL);

directionalLayout.setPadding(32, 32, 32, 32);

Text text = new Text(getContext());

text.setText("My name is Text.");

text.setTextSize(50);

text.setId(100);

// 为组件添加对应布局的布局属性

DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);

layoutConfig.alignment = LayoutAlignment.HORIZONTAL_CENTER;

text.setLayoutConfig(layoutConfig);

// 将Text添加到布局中

directionalLayout.addComponent(text);

// 类似的添加一个Button

Button button = new Button(getContext());

layoutConfig.setMargins(0, 50, 0, 0);

button.setLayoutConfig(layoutConfig);

button.setText("My name is Button.");

button.setTextSize(50);

ShapeElement background = new ShapeElement();

background.setRgbColor(new RgbColor(0, 125, 255));

background.setCornerRadius(25);

button.setBackground(background);

button.setPadding(10, 10, 10, 10);

button.setClickedListener(new Component.ClickedListener() {

@Override

// 在组件中增加对点击事件的检测

public void onClick(Component Component) {

// 此处添加按钮被点击需要执行的操作

}

});

directionalLayout.addComponent(button);

// 将布局作为根布局添加到视图树中

super.setUIContent(directionalLayout);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值