组件代码
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;
}
}
布局样式
<?xml version="1.0" encoding="utf-8"?>
<StackLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:id="$+id:sl"
ohos:height="match_parent"
ohos:width="match_parent">
<com.example.myapplication.FlowComponent
ohos:id="$+id:fc"
ohos:height="match_parent"
ohos:width="match_content"/>
</StackLayout>
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();
}
}
网友评论