希望这里有一个解决方案,我的 main.xml 中有一个 XML TimePicker 和 ScrollView,并且设置后 TimePicker 不会滚动。如果我删除 ScrollView,Timepicker 将滚动得很好,但显然我需要两者。
看起来他们都在为获得触摸事件而战,但这会导致问题。
这是我的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/backgroundmain">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Action Title -->
<ImageView />
<TextView />
<!-- Description -->
<TextView />
<!-- Have to Button -->
<TextView />
<RelativeLayout
android:id="@+id/TimePickerSection"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/">
<TimePicker
android:id="@+id/TimePick"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@id/HaveToText"/>
<Button
android:id="@+id/OkButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/OkText"
android:layout_below="@id/HaveToText"
android:textSize="20sp"
android:onClick="TimePickButton"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/TimePick"/>
</RelativeLayout>
<!-- OR -->
<TextView />
<!-- buttons -->
<TextView />
<Button />
</RelativeLayout>
</ScrollView>
我删除了不相关的代码,但保留了轮廓只是为了向您展示布局的布局方式。
XML 中是否有修复?我尝试过 android:fillViewPort=""
之类的东西,但什么也没做。我的 TimePicker 不是对话框或任何东西的一部分,希望我能保持这种状态。
最佳答案
找到解决方案,创建一个自定义的 TimePicker 类,然后调用它来覆盖触摸事件:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Stop ScrollView from getting involved once you interact with the View
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
关于android - ScrollView 与 TimePicker 的滚动打架,结果 Timepicker 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14354006/