我正在尝试制作一个与维基百科类似的“目录”,如此处所示。
我有一个ScrollView
包含许多TextViews
. 我制作了一个包含可点击元素的“目录”,但我不知道如何找到ScrollView
scrollTo()
正确的位置。
我试图将 转换TextView
为字符串并使用s.IndexOf()
,但它为我提供了字符串中文本的位置,而不是ScrollView
. 这种行为是有道理的
然后,我尝试查看其他ScrollView
方法以找到有用的方法,但没有想法。
如果我能在里面找到IndexOf
那个地方,我可以很容易地找到那个地方,但我不知道该怎么做。TextView
ScrollView
ScrollTo
谢谢, EDIT2:这与超链接到 HTML 中的内联锚非常相似,使用 #如此处所述。
编辑:添加了我的 XML,这似乎是使代码更清晰的唯一重要部分。目标是点击TextView
with idKeyQuestionsTextView
并让它向下滚动到TextView
with id KeyQuestions
。我已经让onclick
监听器KeyQuestionsTextView
注册OnClick
事件,但我不知道如何让ScrollView
滚动到正确的位置。我真的需要在 中获得所需的 Y 偏移量TextView
,ScrollView
因为该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>