アンドロイドで機能する再利用可能なボーダーレイアウトを実装する方法はありますか?スイングのBorderLayoutと同じように動作するもの:中央を最大化し、残りを最小サイズに縮小しますか?
6627 次
3 に答える
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" />
于 2015-04-07T13:49:45.573 に答える
1
属性とRelativeLayout
一緒に使用して、同じ効果を得ることができます。android:layout_weight
于 2011-06-24T20:03:51.170 に答える
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);
}
}
于 2019-07-27T08:59:27.157 に答える