现在我如何使这两个视图成为正方形(例如高度必须是
等于它们的宽度)。
如果您只有这两个视图LinearLayout
你有两个选择:
-
不要将布局文件直接设置为内容视图,而是将其展开,以便您可以引用该文件的根目录。然后发一个Runnable
在该根视图上,计算所需的高度并将其设置回该根视图的两个子视图中的每一个LinearLayout
包裹它们:
final View v = getLayoutInflater().inflate(
R.layout.views_specialheight, null);
v.post(new Runnable() {
@Override
public void run() {
setupHeight((ViewGroup) v);
}
});
setContentView(v);
where setupHeight()
方法是:
private void setupHeight(ViewGroup vg) {
int count = vg.getChildCount();
for (int i = 0; i < count; i++) {
final View v = vg.getChildAt(i);
if (v instanceof LinearLayout) {
int width = v.getWidth();
LinearLayout.LayoutParams lp;
View one = ((LinearLayout) v).getChildAt(0);
lp = (LinearLayout.LayoutParams) one.getLayoutParams();
lp.height = width / 2;
one.setLayoutParams(lp);
View two = ((LinearLayout) v).getChildAt(1);
lp = (LinearLayout.LayoutParams) two.getLayoutParams();
lp.height = width / 2;
two.setLayoutParams(lp);
}
}
}
如果您只有一个包装器,此方法将非常有效ViewGroup
包装这些的子类LinearLayout
行。它可以(而且应该)改进,但我相信您明白这一点。
-
第二个选项是使用自定义ViewGroup
(这可以根据您计划对LinearLayout
)而不是LinearLayout
像这样:
<com.luksprog.oat.views.SpecialHeightViewGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0077cc"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#99cc00"
android:text="Button" />
</com.luksprog.oat.views.SpecialHeightViewGroup>
The SpecialHeightViewGroup
是一个这样的类:
class SpecialHeightViewGroup extends ViewGroup {
public SpecialHeightViewGroup(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public SpecialHeightViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SpecialHeightViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY
|| widthMode == MeasureSpec.AT_MOST) {
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize, widthSize / 2);
} else {
widthSize = 800; // we don't have restrictions, make it as big as we want
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize, 400);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View one = getChildAt(0);
one.layout(0, 0, getWidth() / 2, getHeight());
View two = getChildAt(1);
two.layout(getWidth() / 2, 0, getWidth(), getHeight());
}
}
自定义类未经过正确测试,因此可能存在错误(这只是一个示例)。
题外话:您是否尝试过创建一个像新 Windows 手机上那样的 UI(带有磁贴功能)?