Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的边界布局?_Android_Border Layout - Fatal编程技术网

android中的边界布局?

android中的边界布局?,android,border-layout,Android,Border Layout,有没有一种方法可以在android中实现可重用的边框布局?一个与swing的BorderLayout行为相似的属性:最大化中间部分,并将其余部分减小到最小尺寸?您可以与android:layout\u weight属性一起使用,以获得相同的效果。尝试一下,您将获得与Swings BorderLayout相同的行为(结果如图所示): 我不想使用XML布局文件,我发现在代码中使用RelativeLayout很麻烦。因此,我实现了自己的BorderLayout类: public class Bo

有没有一种方法可以在android中实现可重用的边框布局?一个与swing的BorderLayout行为相似的属性:最大化中间部分,并将其余部分减小到最小尺寸?

您可以与android:layout\u weight属性一起使用,以获得相同的效果。

尝试一下,您将获得与Swings BorderLayout相同的行为(结果如图所示):



我不想使用XML布局文件,我发现在代码中使用RelativeLayout很麻烦。因此,我实现了自己的BorderLayout类:

public class BorderLayout extends ViewGroup {

    private View left;
    private View top;
    private View right;
    private View bottom;
    private View center;

    public BorderLayout(Context context) {
        super(context);
    }

    public void setLeft(View left) {
        if (this.left != null)
            removeView(this.left);
        this.left = left;
        if (this.left != null)
            addView(this.left);
    }

    public void setTop(View top) {
        if (this.top != null)
            removeView(this.top);
        this.top = top;
        if (this.top != null)
            addView(this.top);
    }

    public void setRight(View right) {
        if (this.right != null)
            removeView(this.right);
        this.right = right;
        if (this.right != null)
            addView(this.right);
    }

    public void setBottom(View bottom) {
        if (this.bottom != null)
            removeView(this.bottom);
        this.bottom = bottom;
        if (this.bottom != null)
            addView(this.bottom);
    }

    public void setCenter(View center) {
        if (this.center != null)
            removeView(this.center);
        this.center = center;
        if (this.center != null)
            addView(this.center);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int topWidth = 0;
        int topHeight = 0;
        if (top != null) {
            top.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            topWidth = top.getMeasuredWidth();
            topHeight = top.getMeasuredHeight();
        }
        int bottomWidth = 0;
        int bottomHeight = 0;
        if (bottom != null) {
            bottom.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            bottomWidth = bottom.getMeasuredWidth();
            bottomHeight = bottom.getMeasuredHeight();
        }
        int remainingHeightSpec = GuiUtil.subtractFromMeasureSpec(heightMeasureSpec, topHeight + bottomHeight);
        int leftWidth = 0;
        int leftHeight = 0;
        if (left != null) {
            left.measure(MeasureSpec.UNSPECIFIED, remainingHeightSpec);
            leftWidth = left.getMeasuredWidth();
            leftHeight = left.getMeasuredHeight();
        }
        int rightWidth = 0;
        int rightHeight = 0;
        if (right != null) {
            right.measure(MeasureSpec.UNSPECIFIED, remainingHeightSpec);
            rightWidth = right.getMeasuredWidth();
            rightHeight = right.getMeasuredHeight();
        }
        int remainingWidthSpec = GuiUtil.subtractFromMeasureSpec(widthMeasureSpec, leftWidth + rightWidth);
        int centerWidth = 0;
        int centerHeight = 0;
        if (center != null) {
            center.measure(remainingWidthSpec, remainingHeightSpec);
            centerWidth = center.getMeasuredWidth();
            centerHeight = center.getMeasuredHeight();
        }
        int width = Util.max(topWidth, leftWidth + centerWidth + rightWidth, bottomWidth);
        int height = topHeight + Util.max(leftHeight, centerHeight, rightHeight) + bottomHeight;
        setMeasuredDimension(GuiUtil.getDefaultSize(width, widthMeasureSpec), GuiUtil.getDefaultSize(height, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = r - l;
        int h = b - t;
        int topHeight = 0;
        if (top != null) {
            topHeight = top.getMeasuredHeight();
            top.layout(0, 0, w, topHeight);
        }
        int bottomHeight = 0;
        if (bottom != null) {
            bottomHeight = bottom.getMeasuredHeight();
            bottom.layout(0, h - bottomHeight, w, h);
        }
        int leftWidth = 0;
        if (left != null) {
            leftWidth = left.getMeasuredWidth();
            left.layout(0, topHeight, leftWidth, h - bottomHeight);
        }
        int rightWidth = 0;
        if (right != null) {
            rightWidth = right.getMeasuredWidth();
            right.layout(w - rightWidth, topHeight, w, h - bottomHeight);
        }
        if (center != null)
            center.layout(leftWidth, topHeight, w - rightWidth, h - bottomHeight);
    }

}

你确定有“布局重量”属性吗?(安卓2.1 API级别7)@Bigbohne-From:“公共静态最终整数布局重量:API级别1”。请注意,名称空间限定符
android:
是必不可少的。没有属性
layout\u weight
,但有一个属性
android:layout\u weight
。当您要替换视图程序时,请获取上面文本视图的id和布局参数
public class BorderLayout extends ViewGroup {

    private View left;
    private View top;
    private View right;
    private View bottom;
    private View center;

    public BorderLayout(Context context) {
        super(context);
    }

    public void setLeft(View left) {
        if (this.left != null)
            removeView(this.left);
        this.left = left;
        if (this.left != null)
            addView(this.left);
    }

    public void setTop(View top) {
        if (this.top != null)
            removeView(this.top);
        this.top = top;
        if (this.top != null)
            addView(this.top);
    }

    public void setRight(View right) {
        if (this.right != null)
            removeView(this.right);
        this.right = right;
        if (this.right != null)
            addView(this.right);
    }

    public void setBottom(View bottom) {
        if (this.bottom != null)
            removeView(this.bottom);
        this.bottom = bottom;
        if (this.bottom != null)
            addView(this.bottom);
    }

    public void setCenter(View center) {
        if (this.center != null)
            removeView(this.center);
        this.center = center;
        if (this.center != null)
            addView(this.center);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int topWidth = 0;
        int topHeight = 0;
        if (top != null) {
            top.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            topWidth = top.getMeasuredWidth();
            topHeight = top.getMeasuredHeight();
        }
        int bottomWidth = 0;
        int bottomHeight = 0;
        if (bottom != null) {
            bottom.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            bottomWidth = bottom.getMeasuredWidth();
            bottomHeight = bottom.getMeasuredHeight();
        }
        int remainingHeightSpec = GuiUtil.subtractFromMeasureSpec(heightMeasureSpec, topHeight + bottomHeight);
        int leftWidth = 0;
        int leftHeight = 0;
        if (left != null) {
            left.measure(MeasureSpec.UNSPECIFIED, remainingHeightSpec);
            leftWidth = left.getMeasuredWidth();
            leftHeight = left.getMeasuredHeight();
        }
        int rightWidth = 0;
        int rightHeight = 0;
        if (right != null) {
            right.measure(MeasureSpec.UNSPECIFIED, remainingHeightSpec);
            rightWidth = right.getMeasuredWidth();
            rightHeight = right.getMeasuredHeight();
        }
        int remainingWidthSpec = GuiUtil.subtractFromMeasureSpec(widthMeasureSpec, leftWidth + rightWidth);
        int centerWidth = 0;
        int centerHeight = 0;
        if (center != null) {
            center.measure(remainingWidthSpec, remainingHeightSpec);
            centerWidth = center.getMeasuredWidth();
            centerHeight = center.getMeasuredHeight();
        }
        int width = Util.max(topWidth, leftWidth + centerWidth + rightWidth, bottomWidth);
        int height = topHeight + Util.max(leftHeight, centerHeight, rightHeight) + bottomHeight;
        setMeasuredDimension(GuiUtil.getDefaultSize(width, widthMeasureSpec), GuiUtil.getDefaultSize(height, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = r - l;
        int h = b - t;
        int topHeight = 0;
        if (top != null) {
            topHeight = top.getMeasuredHeight();
            top.layout(0, 0, w, topHeight);
        }
        int bottomHeight = 0;
        if (bottom != null) {
            bottomHeight = bottom.getMeasuredHeight();
            bottom.layout(0, h - bottomHeight, w, h);
        }
        int leftWidth = 0;
        if (left != null) {
            leftWidth = left.getMeasuredWidth();
            left.layout(0, topHeight, leftWidth, h - bottomHeight);
        }
        int rightWidth = 0;
        if (right != null) {
            rightWidth = right.getMeasuredWidth();
            right.layout(w - rightWidth, topHeight, w, h - bottomHeight);
        }
        if (center != null)
            center.layout(leftWidth, topHeight, w - rightWidth, h - bottomHeight);
    }

}