0

我有这种情况:填充容器的水平 LinearLayout 和weightSum=100,以及内部的两个视图,每个视图的重量为 50。

现在我如何使这两个视图呈正方形(例如高度必须等于它们的宽度)。LinearLayout 的行数是未知的,所以基本上,在这种情况下,我不能将它们包装在带有权重的垂直容器中。

4

1 回答 1

1

现在我如何使这两个视图呈正方形(例如高度必须等于它们的宽度)。

如果您只有这两个视图,LinearLayout您有两个选择:

  1. 不要将您的布局文件直接设置为内容视图,而是将其膨胀,以便您可以引用该文件的根目录。然后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);
    

    方法在哪里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行的包装器子类,则此方法将工作得很好。它可以(并且应该)改进,但我相信你明白这一点。

  2. 第二个选项是使用自定义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>
    

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(带有瓷砖的东西)?

于 2012-09-20T15:13:54.943 回答