有没有办法在Android中实现工作可重用的边框布局?一个行为就像swing的BorderLayout:最大化中间并将其余部分缩小到最小尺寸?
答案 0 :(得分:6)
试试这个,你会得到与Swings BorderLayout相同的行为(结果如图所示):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/north"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/holo_orange_light"
android:gravity="center_horizontal"
android:text="North"
android:textAppearance="@android:style/TextAppearance.Large" />
<TextView
android:id="@+id/south"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/holo_blue_light"
android:gravity="center_horizontal"
android:text="South"
android:textAppearance="@android:style/TextAppearance.Large" />
<TextView
android:id="@+id/west"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="@id/south"
android:layout_alignParentLeft="true"
android:layout_below="@id/north"
android:background="@android:color/holo_red_light"
android:gravity="center_vertical"
android:text="West"
android:textAppearance="@android:style/TextAppearance.Large" />
<TextView
android:id="@+id/east"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="@id/south"
android:layout_alignParentRight="true"
android:layout_below="@id/north"
android:background="@android:color/holo_purple"
android:gravity="center_vertical"
android:text="East"
android:textAppearance="@android:style/TextAppearance.Large" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/south"
android:layout_below="@id/north"
android:layout_toLeftOf="@id/east"
android:layout_toRightOf="@id/west"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:text="Center"
android:textAppearance="@android:style/TextAppearance.Large" />
答案 1 :(得分:1)
您可以使用RelativeLayout
和android:layout_weight
属性来获得相同的效果。
答案 2 :(得分:0)
我不想使用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);
}
}