我正在开发我的第一个应用程序并让它像我想要的那样工作,只有一个小例外。问题出在scrollview中,相对布局最多包含20个按钮。当我向下滚动并单击底部附近的按钮时,滚动视图会在单击后滚动回到顶部。我希望scrollview在点击后保持原样。例如,在单击按钮18之后,我不希望用户必须向下滚动到底部以单击按钮19.有人可以告诉我如何在单击后停止滚动视图自动滚动到顶部。
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/timerScrollView"
android:layout_below="@+id/startStopBtn"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
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" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner1Btn"
android:layout_marginTop="5dp"
android:layout_below="@+id/startStopBtn"
android:layout_alignParentStart="true"
android:layout_marginLeft="0dp"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner1TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner1Btn"
android:visibility="gone"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner2Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:isScrollContainer="true"
android:keepScreenOn="true"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/runner1Btn"
android:layout_marginTop="15dp"
android:layout_below="@+id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner3Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="false"
android:layout_below="@+id/runner2Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner4Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner3Btn"
android:layout_alignParentStart="false"
android:isScrollContainer="false"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner5Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner4Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner6Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner5Btn"
android:layout_alignStart="@id/runner1Btn"
android:layout_marginTop="15dp"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner7Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner6Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner8Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:isScrollContainer="true"
android:keepScreenOn="true"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/runner1Btn"
android:layout_marginTop="15dp"
android:layout_below="@+id/runner7Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner9Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="false"
android:layout_below="@+id/runner8Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner10Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner9Btn"
android:layout_alignParentStart="false"
android:isScrollContainer="false"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner11Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner10Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner12Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner11Btn"
android:layout_alignStart="@id/runner1Btn"
android:layout_marginTop="15dp"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner13Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner12Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner14Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner13Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner15Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:isScrollContainer="true"
android:keepScreenOn="true"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/runner1Btn"
android:layout_marginTop="15dp"
android:layout_below="@+id/runner14Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner16Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="false"
android:layout_below="@+id/runner15Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner17Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner16Btn"
android:layout_alignParentStart="false"
android:isScrollContainer="false"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner18Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner17Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone"
android:focusableInTouchMode="false" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner19Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner18Btn"
android:layout_alignStart="@id/runner1Btn"
android:layout_marginTop="15dp"
android:visibility="gone" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="40dp"
android:id="@+id/runner20Btn"
android:background="#101fbd"
android:textColor="#FFFFFFFF"
android:textSize="20dp"
android:layout_below="@+id/runner19Btn"
android:layout_marginTop="15dp"
android:layout_alignStart="@id/runner1Btn"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner2TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner2Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner3TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner3Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner4TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner4Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner5TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner5Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner6TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner6Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner7TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner7Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner8TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner8Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner9TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner9Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner10TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner10Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner11TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner11Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner12TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner12Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner13TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner13Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner14TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner14Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner15TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner15Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner16TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner16Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner17TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner17Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner18TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner18Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner19TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner19Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/runner20TimeView"
android:layout_marginRight="0dp"
android:textColor="#000000"
android:textSize="30dp"
android:textIsSelectable="true"
android:layout_alignBaseline="@+id/runner20Btn"
android:visibility="gone"
android:layout_alignEnd="@id/runner1TimeView"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</ScrollView>
以下是其中一个按钮的OnClickListener的JAVA代码。
runner[18].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (runnerRunning[18]) {
time[18] = System.currentTimeMillis() - startTime;
runners.get(18).setTime(formatTime(time[18]));
runnerTime[18].setText(formatTime(time[18]));
runnerRunning[18] = false;
}
}
});