为此,我尝试创建一个布局。
它就像我在原始问题中讨论的那样工作,但如果你做得更好,请随时更新答案!
public class KeyValueLayout extends ViewGroup {
public KeyValueLayout(Context context) {
super(context);
}
public KeyValueLayout(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public KeyValueLayout(Context context, AttributeSet attributeSet,
int defStyle) {
super(context, attributeSet, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int paddRight = getPaddingRight();
int paddLeft = getPaddingLeft();
int paddBottom = getPaddingBottom();
int paddWidth = paddRight + paddLeft;
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - paddWidth;
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - paddWidth;
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int modeW = modeWidth == MeasureSpec.EXACTLY
? MeasureSpec.AT_MOST : modeWidth;
int modeH = modeHeight == MeasureSpec.EXACTLY
? MeasureSpec.AT_MOST : modeHeight;
// where the next view should be placed
int lineYPosition = 0;
for (int i = 0; i < getChildCount(); ) {
View key = getChildAt(i++);
key.measure(
MeasureSpec.makeMeasureSpec(sizeWidth, modeW),
MeasureSpec.makeMeasureSpec(sizeHeight, modeH)
);
LayoutParams keyLp = (LayoutParams) key.getLayoutParams();
keyLp.setPosition(paddLeft, lineYPosition);
if (i >= getChildCount())
break;
View val = getChildAt(i++);
val.measure(
MeasureSpec.makeMeasureSpec(sizeWidth, modeW),
MeasureSpec.makeMeasureSpec(sizeHeight, modeH)
);
LayoutParams valLp = (LayoutParams) val.getLayoutParams();
int valXPosition = paddLeft + sizeWidth - val.getMeasuredWidth();
// check if both fit on the same line
if (key.getMeasuredWidth() + val.getMeasuredWidth() <= sizeWidth) {
valLp.setPosition(valXPosition, lineYPosition);
lineYPosition += Math.max(key.getMeasuredHeight(),
val.getMeasuredHeight());
} else {
// not enough room, make some space
lineYPosition += key.getMeasuredHeight();
valLp.setPosition(valXPosition, lineYPosition);
lineYPosition += val.getMeasuredHeight();
}
}
int controlMaxLength = sizeWidth + paddRight;
int controlMaxThickness = lineYPosition + paddBottom;
int resolvedWidth = resolveSize(controlMaxLength, widthMeasureSpec);
int resolvedHeight =resolveSize(controlMaxThickness,heightMeasureSpec);
this.setMeasuredDimension(resolvedWidth, resolvedHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y,
lp.x + child.getMeasuredWidth(),
lp.y + child.getMeasuredHeight());
}
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
return new LayoutParams(getContext(), attributeSet);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
public static class LayoutParams extends ViewGroup.LayoutParams {
private int x;
private int y;
public LayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams layoutParams) {
super(layoutParams);
}
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
}
}
示例用法:
<nu.wen.android.layout.KeyValueLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_string_long"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_string_short" />
</nu.wen.android.layout.KeyValueLayout>