通过仅知道其宽度权重来设置视图的高度以使其成为正方形

2024-04-02

我有这样的场景: 填充容器的水平 LinearLayoutweightSum=100,以及内部的两个视图,每个视图的权重为 50。

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


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

如果您只有这两个视图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);
    

    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行。它可以(而且应该)改进,但我相信您明白这一点。

  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>
    

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(带有磁贴功能)?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过仅知道其宽度权重来设置视图的高度以使其成为正方形 的相关文章

随机推荐