在您的自定义布局中,创建一个扩展ViewGroup.LayoutParams
的嵌套类。然后重写一些方法(所有必需的都在我的例子中)。这里是我的自定义布局中的一个精简版本:
public class MyLayout extends ViewGroup {
public MyLayout(Context context) {
}
public MyLayout(Context context, AttributeSet attrs) {
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return generateDefaultLayoutParams(); // TODO Change this?
}
public static class LayoutParams extends ViewGroup.LayoutParams {
public LayoutParams() {
}
public LayoutParams(int width, int height) {
}
public LayoutParams(Context context, AttributeSet attrs) {
}
}
}
进一步解释:How to create a FlowLayout(!感谢您的链接Luksprog)
请参阅此链接(有关的FlowLayout最后部分):HTTP:/ /www.parleys.com/#st=5&id=2191&sl=38。 – Luksprog 2012-08-15 14:57:17
伟大的链接!完善! – Jona 2012-08-15 18:10:05
上面的链接不起作用。这一个可能会有所帮助:https://github.com/ApmeM/android-flowlayout – 2017-07-24 13:26:02