【问题标题】:How can I make an android ViewGroup and all its child Views dynamically maintain equal width based on the widest child?如何使 android ViewGroup 及其所有子视图根据最宽的子视图动态保持相等的宽度?
【发布时间】:2013-09-29 15:03:17
【问题描述】:

我希望我的 android 布局表现得像这样: 假设 SomeLayout 是 Widget1、Layout2 和 Widget2 的父级,它们按从上到下的顺序显示,如下所示:

|-------SomeLayout-------|
||--------Widget1-------||
||______________________||
||--------Layout2-------||
||______________________||
||--------Widget2-------||
||______________________||
|________________________|

这 3 个孩子的内容正在发生变化。随着它们的动态变化,我总是希望拥有最广泛内容的内容包含在内容中。我希望 SomeLayout 环绕那个最宽的孩子,我希望所有其他孩子都匹配那个最宽的孩子。即使哪一个是最广泛的变化。

一段时间以来,我一直在尝试使用比我数不清的更多方法来完成此任务。所有的失败。有谁知道如何通过 android 布局 XML 实现这种效果?如果这是任何人都可以提供的,我愿意使用 Java 编程解决方案,但我更愿意仅通过 XML 来实现。

【问题讨论】:

    标签: android layout viewgroup


    【解决方案1】:

    好问题,伙计。 开始检查后,我觉得这是一个很好的问题..

    这就是你想要的..看看..

    它只是一个简单的.. 将一些布局宽度应用于 wrap_content 并将所有直接子级的宽度应用于 match_parent..

    请参见下面的示例。您可以将其指定为背景颜色。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <!-- This is Your Some Layout -->
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <!-- Widget1 -->
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2431234vb sset " />
    
            <!-- Your Layout 2 (inner one) -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2431234v " />
            </LinearLayout>
    
            <!-- Widget2 -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2431234vb sset " />
        </LinearLayout>
    
    </LinearLayout>
    

    希望对您有所帮助..

    【讨论】:

    • 谢谢,正是我想要的:)
    【解决方案2】:

    我希望您的 SomeLayout 扩展 ViewGroup,所以我会尝试覆盖 onMeasure 方法,以便以适当的方式测量所有子项。 例如

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int maxWidth = 0;
        int maxIndex = 0;
        for (int i = 0; i < getChildCount(); i++){
            View child = getChildAt(i);
            final int widthSpec = MeasureSpec.makeMeasureSpec(
                        specWidth, MeasureSpec.UNSPECIFIED);
            final int heightSpec = MeasureSpec.makeMeasureSpec(
                        0, MeasureSpec.UNSPECIFIED);
            child.measure(widthSpec, heightSpec);
            int width = child.getMeasuredWidth();
            if (width > maxWidth){
                maxWidth = width;
                maxIndex = i;
            }
        }
        for (int i = 0; i < getChildCount(); i++){
            if (i != maxIndex){
                View child = getChildAt(i);
                final int widthSpec = MeasureSpec.makeMeasureSpec(
                        maxWidth, MeasureSpec.EXACTLY);
                final int heightSpec = MeasureSpec.makeMeasureSpec(
                        0, MeasureSpec.UNSPECIFIED);
                child.measure(widthSpec, heightSpec);
            }
        }
        setMeasuredDimension(someMeasuredWidth, someMeasuredHeight);
    }
    

    因此,您应该定义最宽的孩子并测量所有其他孩子以调整最大尺寸。之后,您应该根据测量的尺寸在 onLayout 方法实现中布局所有子项。

    希望对你有帮助

    【讨论】:

    • 我来这里是为了寻找一种方法来使所有子布局的高度相同,并且我设法通过使用您的方法来做到这一点,但是我不得不删除最后一行 setMeasuredDimension(someMeasuredWidth, someMeasuredHeight); 并且我还添加了第二行super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2016-09-05
    相关资源
    最近更新 更多