ScrollView溢出其FrameLayout父

时间:2016-05-20 04:39:46

标签: android android-layout scrollview android-framelayout overlapping

我有一个复杂的LinearLayout(垂直),FrameLayout高于其他一些东西(TextViewEditText框。 FrameLayout包含两件事:a)地图和b)包含半透明ScrollView的{​​{1}},其'覆盖'地图。 ScrollView最初是LinearLayout,但我以编程方式呈现它。我还以编程方式将视图添加到GONE中嵌入的LinearLayout

使用ScrollView设置FrameLayout尺寸,以使高度和宽度相等。这会使onMeasure()占据设备屏幕的顶部,同时FrameLayoutTextView适合下方。

EditText

一开始真的很棒。但是,当我向public static class MyFrameLayout extends FrameLayout { public FrameLayoutWithMap(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); int size = Math.min(getMeasuredWidth(), getMeasuredHeight()); setMeasuredDimension(size, size); } } 中嵌入的LinearLayout添加足够的视图时,ScrollView最终会将延伸到ScrollView TextView以下FrameLayout。换句话说,ScrollView溢出其FrameLayout父级,然后隐藏其底部。当我继续向LinearLayout中嵌入的ScrollView添加视图时,ScrollView将最终开始滚动。一旦ScrollView是设备屏幕的高度,就会发生这种情况。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activityRoot"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#323232"
    tools:context=".MainActivity" >

    <FrameLayout
        class="com.myapp.main.MyFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#070707" >

        <com.myapp.main.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ScrollView
            android:id="@+id/overlayScrollView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_gravity="left"
            android:scrollbars="vertical"
            android:fillViewport="false"
            android:visibility="gone" >
            <LinearLayout
                android:id="@+id/overlayLinearLayout"
                android:orientation="vertical"
                android:layout_width="100dp"
                android:layout_height="wrap_content"                
                android:padding="1dp"       
                android:background="#AA000000" >
            </LinearLayout>
        </ScrollView>

    </FrameLayout>

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textColor="#FFFFFF"
        android:textSize="14sp" />

    <EditText android:id="@+id/entry"
        android:background="@drawable/entry_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="1"
        android:singleLine="true"
        android:textSize="16sp"
        android:textColor="#ffffff"
        android:hint="@string/entry" />

</LinearLayout>

我希望发生的是ScrollView一旦它是FrameLayout父级的高度就开始滚动。换句话说,我希望ScrollView在其FrameLayout父级中保留

enter image description here

知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

<强>更新

新的ConstraintLayout库可让您精确控制视图相对于父视图和其他视图的大小。它还支持宽高比,因此您可以(例如)强制视图始终为方形。

原始回复:

问题是如何测量特殊的FrameLayout。当您致电super.onMeasure()时,此视图及其所有子项将根据给出的度量规范进行自我衡量。之后,你约束了这个视图的尺寸,但它的孩子都没有意识到这一点;只有这个视图及其才知道这一点。它的孩子们已经测量了自己认为这个视图的大小不受你setMeasuredDimension()的额外调用的限制。

再次致电super.onMeasure()将迫使其子女再次测量自己,从而告知他们新的尺寸。

@Override
public void onMeasure(int widthSpec, int heightSpec) {
    super.onMeasure(widthSpec, heightSpec);
    int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    int spec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
    super.onMeasure(spec, spec);
}

请注意,这会导致此视图及其所有子视图在每次测量过程中测量两次,因此它可能不是最高效的方法,但它可以实现您想要的效果。