鸿蒙基于liu,鸿蒙学习-流式布局

组件代码

package com.example.myapplication;

import ohos.agp.components.AttrSet;

import ohos.agp.components.Component;

import ohos.agp.components.ComponentContainer;

import ohos.app.Context;

import java.util.HashMap;

import java.util.Map;

public class FlowComponentextends ComponentContainerimplements Component.EstimateSizeListener, ComponentContainer.ArrangeListener {

private int yy =0;

private int xx =0;

private int maxWidth =0;

private int maxHeight =0;

private int lastHeight =0;

// 子组件索引与其布局数据的集合

private final Mapaxis =new HashMap<>();

public FlowComponent(Context context) {

super(context);

setEstimateSizeListener(this);

setArrangeListener(this);

}

public FlowComponent(Context context, AttrSet attrSet) {

super(context, attrSet);

setEstimateSizeListener(this);

setArrangeListener(this);

}

public FlowComponent(Context context, AttrSet attrSet, String styleName) {

super(context, attrSet, styleName);

setEstimateSizeListener(this);

setArrangeListener(this);

}

//测量组件的大小以确定宽度和高度。

@Override

public boolean onEstimateSize(int widthEstimatedConfig,int heightEstimatedConfig) {

measureChildren(widthEstimatedConfig, heightEstimatedConfig);

int width = Component.EstimateSpec.getSize(widthEstimatedConfig);

// 关联子组件的索引与其布局数据

for (int idx =0; idx < getChildCount(); idx++) {

Component childView = getComponentAt(idx);

addChild(childView, idx, width);

}

setEstimatedSize(

Component.EstimateSpec.getChildSizeWithMode(maxWidth, widthEstimatedConfig,0),

Component.EstimateSpec.getChildSizeWithMode(maxHeight, heightEstimatedConfig,0));

return true;

}

private void measureChildren(int widthEstimatedConfig,int heightEstimatedConfig) {

for (int idx =0; idx < getChildCount(); idx++) {

Component childView = getComponentAt(idx);

if (childView !=null) {

measureChild(childView, widthEstimatedConfig, heightEstimatedConfig);

}

}

}

private void measureChild(Component child,int parentWidthMeasureSpec,int parentHeightMeasureSpec) {

ComponentContainer.LayoutConfig lc = child.getLayoutConfig();

int childWidthMeasureSpec = EstimateSpec.getChildSizeWithMode(

lc.width, parentWidthMeasureSpec, EstimateSpec.UNCONSTRAINT);

int childHeightMeasureSpec = EstimateSpec.getChildSizeWithMode(

lc.height, parentHeightMeasureSpec, EstimateSpec.UNCONSTRAINT);

child.estimateSize(childWidthMeasureSpec, childHeightMeasureSpec);

}

private void invalidateValues() {

xx =0;

yy =0;

maxWidth =0;

maxHeight =0;

axis.clear();

}

private void addChild(Component component,int id,int layoutWidth) {

Layout layout =new Layout();

layout.positionX =xx + component.getMarginLeft();

layout.positionY =yy + component.getMarginTop();

layout.width = component.getEstimatedWidth();

layout.height = component.getEstimatedHeight();

if ((xx + layout.width) > layoutWidth) {

xx =0;

yy +=lastHeight;

lastHeight =0;

layout.positionX =xx + component.getMarginLeft();

layout.positionY =yy + component.getMarginTop();

}

axis.put(id, layout);

lastHeight = Math.max(lastHeight, layout.height + component.getMarginBottom());

xx += layout.width + component.getMarginRight();

maxWidth = Math.max(maxWidth, layout.positionX + layout.width);

maxHeight = Math.max(maxHeight, layout.positionY + layout.height);

}

@Override

public boolean onArrange(int i,int i1,int i2,int i3) {

// 对各个子组件进行布局

for (int idx =0; idx < getChildCount(); idx++) {

Component childView = getComponentAt(idx);

Layout layout =axis.get(idx);

if (layout !=null) {

childView.setTag(layout.toString());

childView.arrange(layout.positionX, layout.positionY, layout.width, layout.height);

}

}

return true;

}

private static class Layout {

int positionX =0;

int positionY =0;

int width =0;

int height =0;

}

}

布局样式

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:id="$+id:sl"

ohos:height="match_parent"

ohos:width="match_parent">

ohos:id="$+id:fc"

ohos:height="match_parent"

ohos:width="match_content"/>

AbilitySlice代码

package com.example.myapplication.slice;

import com.example.myapplication.FlowComponent;

import com.example.myapplication.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

import ohos.agp.colors.RgbColor;

import ohos.agp.components.*;

import ohos.agp.components.element.ShapeElement;

import ohos.agp.utils.Color;

import ohos.agp.utils.LayoutAlignment;

import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySliceextends AbilitySlice {

String[]str = {"yi","er","san","si","wu","liu","qi","ba","jiu","shi","111111111111111111","222222222222222222222","3333","4444444","5555555555"};

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

FlowComponent fc = (FlowComponent) findComponentById(ResourceTable.Id_fc);

for (int i =0; i

fc.addComponent(getText(str[i]));

}

}

public Text getText(String msg) {

Text text =new Text(getContext());

text.setText(msg);

text.setTextSize(50);

text.setClickedListener(new Component.ClickedListener() {

@Override

public void onClick(Component component) {

showToast(msg);

}

});

text.setTextColor(Color.WHITE);

text.setMarginsLeftAndRight(30,30);

text.setMarginsTopAndBottom(15,15);

text.setPadding(10,10,10,10);

text.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_MIDDLE);

ShapeElement element =new ShapeElement();

element.setRgbColor(new RgbColor(78,88,99));

element.setCornerRadius(15);

text.setBackground(element);

return text;

}

public void showToast(String msg) {

ToastDialog dialog =new ToastDialog(getContext());

dialog.setAlignment(LayoutAlignment.CENTER);

dialog.setText(msg);

dialog.show();

}

}

e4dd5278099e

e4dd5278099e

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值