[英]I can't scroll my customized view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<ScrollView android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<adam.music.testfont.NwcDrawingArea android:id="@+id/NwcDrawingArea"
android:layout_height="2000dp"
android:layout_width="2000dp"
/>
</ScrollView>
</LinearLayout>
我创建了 Android 项目并如上所述编辑了 main.xml。 然后我自定义视图来绘制图像。 要查看滚动自定义视图的效果,我如上所述设置了宽度和高度。 现在我可以在自定义视图中看到图像,但它不会滚动。 你能帮我解决什么问题吗? 我应该添加更多内容吗?
ScrollView 与其内容之间存在相互作用。 例如,如果您将这个 TextView 插入到 ScrollView 中,而不是 NwcDrawingArea 小部件:
<TextView android:text="8This is a lot of text 7This is a lot of text 6This is a lot of text 5This is a lot of text 4This is a lot of text 3This is a lot of text 2This is a lot of text 1This is a lot of text"
android:layout_width="10dp"
android:layout_height="2000dp"
android:background="#FF00FF00"
/>
您将看到一个细长的绿色垂直 TextView,其文本比屏幕长,并且 ScrollView 显示滚动条,让您可以看到 TextView 的隐藏部分到文本的范围。 现在,更改 TextView layout_width="2000dp"。 TextView 变成一个全屏绿色区域,其中有一行文本从屏幕右侧延伸。 当然,ScrollView 不显示水平滚动条。 然而,ScrollView 也没有显示垂直滚动条,即使我们将 TextView 的大小设置为比屏幕长得多。 ScrollView 试图确定其内容中视觉上有趣的部分,因此您需要了解您要子类化的任何小部件的行为。
例如,ScrollView 按照您期望它与 TextView 的行为方式尊重 LinearLayout 和 RelativeLayout 的布局大小。 事实上,通常将 LinearLayout 或 RelativeLayout ——而不是像 TextView 这样的视图——作为 ScrollView 的子级。 将 TextView 放入固定高度的 LinearLayout 中,您应该会得到预期的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.