【发布时间】:2013-05-28 07:56:24
【问题描述】:
我正在尝试制作一个与wikipedias as shown here. 感觉相似的“目录”
我有一个ScrollView,其中包含许多TextViews。我制作了一个包含可点击元素的“目录”,但我无法弄清楚如何将ScrollViewscrollTo() 放在正确的位置。
我试图将TextView 转换为字符串,并使用s.IndexOf(),但它给了我字符串中文本的位置,而不是ScrollView。这种行为是有道理的
然后我尝试查看其他 ScrollView 方法以找到有用的方法,但没有任何想法。
如果我能在ScrollView 中找到IndexOf 和TextView,我可以很容易地找到ScrollTo,但我不知道该怎么做。
谢谢, EDIT2:这与超链接到 HTML 中的内联锚非常相似,使用 #as described here.
编辑:添加了我的 XML,这似乎是使代码更清晰的唯一重要部分。目标是使用 id KeyQuestionsTextView 访问 TextView,然后向下滚动到 id 为 KeyQuestions 的 TextView。我已经让KeyQuestionsTextView 的onclick 监听器注册了OnClick 事件,但我不知道如何让ScrollView 滚动到正确的位置。我真的需要在ScrollView 中获取所需TextView 的Y 偏移量,因为ScrollTo 方法看起来很简单。
上面的示例将针对PhysicalExamTextView DiagnosticReasoning 和Emergencies 重复。这四个元素组成了我的“目录”。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Platform2_option1" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Intro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Plat2Option1Intro" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<TextView
android:id="@+id/KeyQuestionsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/KeyQuestionsTitle" />
<TextView
android:id="@+id/PhysicalExamTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PhysicalExamTitle" />
<TextView
android:id="@+id/DiagnosticReasoningTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DiagnosticReasoningTitle" />
<TextView
android:id="@+id/EmergenciesTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/EmergenciesTitle" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<TextView
android:id="@+id/KeyQuestions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Plat2Option1KeyQuestions" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<TextView
android:id="@+id/PhysicalExam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Plat2Option1PhysicalExam" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<TextView
android:id="@+id/DiagnosticReasoning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Plat2Option1DiagReasoning" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<TextView
android:id="@+id/Emergencies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Plat2Option1Emergencies" />
</LinearLayout>
</ScrollView>
【问题讨论】:
标签: android