在您的自定義佈局中,創建一個擴展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