【发布时间】:2016-07-27 20:24:32
【问题描述】:
我有一个自定义视图,TriggerView。当我在视图中覆盖onDraw 时,我绘制的图形有时会溢出视图的可视范围之外。所以我想将视图嵌入到 ScrollView 中,以便图形可以滚动。
这是我的代码,没有任何ScrollViews:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.roberts.croberts.mantis.ShotTriggerFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clipChildren="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageContainer"
android:orientation="horizontal"
android:layout_weight="1">
<com.roberts.croberts.mantis.TriggerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/triggerView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
...
</LinearLayout>
</LinearLayout>
在此屏幕截图中查看 triggerView 如何填充所有剩余的屏幕空间:
然后我尝试将其嵌入到 ScrollView 中:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageContainer"
android:orientation="horizontal">
<com.roberts.croberts.mantis.TriggerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/triggerView" />
</LinearLayout>
</ScrollView>
然后 TriggerView 高度缩小到 0。我已经尝试了 match_parent 和 wrap_content 的所有变体,我能想到,但无法让它工作:
编辑 - 我刚刚发现的另一件有趣的事情是当我删除容器 ScrollView 和 LinearLayout 并只拥有 TriggerView 时:
<com.roberts.croberts.mantis.TriggerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/triggerView"
android:layout_weight="1"/>
然后设计标签说父类是ScrollView?:
【问题讨论】:
-
没有显示您在
onDraw中覆盖的内容,并且可能与测量相关的方法,您可能只会得到“盲拍”答案......您的代码<LinearLayout android:layout_height="0dp"和您的自定义视图中也有,所以很明显它具有相同的高度......
标签: android android-linearlayout android-custom-view android-scrollview