ScrollView 溢出其 FrameLayout 父级
ScrollView overflowing its FrameLayout parent
我有一个复杂的 LinearLayout
(垂直),在其他一些东西(TextView
和 EditText
框上方有一个 FrameLayout
。 FrameLayout
包含两件事:a) 地图和 b) ScrollView
包含半透明 LinearLayout
即 'overlays' 地图。 ScrollView 最初是 GONE
,但我以编程方式使其可见。我还以编程方式将视图添加到 ScrollView
中嵌入的 LinearLayout
。
使用 onMeasure()
设置 FrameLayout 尺寸,以便高度和宽度相等。这使得 FrameLayout
占据设备屏幕的顶部,而 TextView
和 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);
}
}
一开始一切都很好。但是,当我向 ScrollView
中嵌入的 LinearLayout
添加足够的视图时,ScrollView 最终将扩展 behind TextView
below FrameLayout
.换句话说,ScrollView 溢出其 FrameLayout
父级,然后隐藏其底部。当我继续向 ScrollView
中嵌入的 LinearLayout
添加视图时,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
父级中保持 contained。
知道我做错了什么吗?
更新:
新的 ConstraintLayout 库使您可以 fine-grained 控制视图相对于 parent 和其他视图的大小。它还支持宽高比,因此您可以(例如)强制视图始终为正方形。
原回复:
问题在于您如何测量特殊的 FrameLayout。当您调用 super.onMeasure()
时,此视图及其所有 children 将根据给定的度量规范进行自我度量。之后,您限制了该视图的尺寸,但其 children 的 none 知道这一点;只有此视图及其 parent 知道这一点。它的 children 已经测量了自己,认为此视图的大小不受您对 setMeasuredDimension()
.
的额外调用的限制
再次调用 super.onMeasure()
将强制其 children 再次测量自己,从而通知他们新的尺寸。
@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);
}
请注意,这将导致此视图及其所有 children 在每次测量过程中测量两倍的次数,因此它可能不是最有效的方法,但它会完成您想要的。
我有一个复杂的 LinearLayout
(垂直),在其他一些东西(TextView
和 EditText
框上方有一个 FrameLayout
。 FrameLayout
包含两件事:a) 地图和 b) ScrollView
包含半透明 LinearLayout
即 'overlays' 地图。 ScrollView 最初是 GONE
,但我以编程方式使其可见。我还以编程方式将视图添加到 ScrollView
中嵌入的 LinearLayout
。
使用 onMeasure()
设置 FrameLayout 尺寸,以便高度和宽度相等。这使得 FrameLayout
占据设备屏幕的顶部,而 TextView
和 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);
}
}
一开始一切都很好。但是,当我向 ScrollView
中嵌入的 LinearLayout
添加足够的视图时,ScrollView 最终将扩展 behind TextView
below FrameLayout
.换句话说,ScrollView 溢出其 FrameLayout
父级,然后隐藏其底部。当我继续向 ScrollView
中嵌入的 LinearLayout
添加视图时,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
父级中保持 contained。
知道我做错了什么吗?
更新:
新的 ConstraintLayout 库使您可以 fine-grained 控制视图相对于 parent 和其他视图的大小。它还支持宽高比,因此您可以(例如)强制视图始终为正方形。
原回复:
问题在于您如何测量特殊的 FrameLayout。当您调用 super.onMeasure()
时,此视图及其所有 children 将根据给定的度量规范进行自我度量。之后,您限制了该视图的尺寸,但其 children 的 none 知道这一点;只有此视图及其 parent 知道这一点。它的 children 已经测量了自己,认为此视图的大小不受您对 setMeasuredDimension()
.
再次调用 super.onMeasure()
将强制其 children 再次测量自己,从而通知他们新的尺寸。
@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);
}
请注意,这将导致此视图及其所有 children 在每次测量过程中测量两倍的次数,因此它可能不是最有效的方法,但它会完成您想要的。