【问题标题】:I can't scroll my customized view我无法滚动我的自定义视图
【发布时间】:2011-07-21 17:38:00
【问题描述】:
<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。
然后我自定义视图来绘制图像。
要查看滚动自定义视图的效果,我如上所述设置了宽度和高度。
现在我可以在自定义视图中看到图像,但它不会滚动。
你能帮我解决什么问题吗?
我应该添加更多内容吗?
【问题讨论】:
标签:
java
android
android-layout
view
scrollview
【解决方案1】:
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 尊重 LinearLayout 和 RelativeLayout 的布局大小,就像您期望它与 TextView 一样。事实上,将 LinearLayout 或 RelativeLayout ——而不是像 TextView 这样的视图——作为 ScrollView 的子级是很常见的。将您的 TextView 放到一个固定高度的 LinearLayout 中,您应该会得到您所期望的行为。