カスタムレイアウトでは、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
すばらしいリンク!完璧! – Jona
上記のリンクが機能していません。これは参考になるかもしれません:https://github.com/ApmeM/android-flowlayout –