希望这里有一个解决方案,我的 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)