防止从屏幕上的任何位置访问HorizontalScrollView 我在Android工作室中创建了一个水平滚动视图,它可以从屏幕上的任何地方访问,也就是说,无论我在哪里滚动,无论是在屏幕的顶部还是底部,中间的滚动视图都会滚动。
代码如下:防止从屏幕上的任何位置访问HorizontalScrollView 我在Android工作室中创建了一个水平滚动视图,它可以从屏幕上的任何地方访问,也就是说,无论我在哪里滚动,无论是在屏幕的顶部还是底部,中间的滚动视图都会滚动。,android,xml,android-studio,scrollview,horizontalscrollview,Android,Xml,Android Studio,Scrollview,Horizontalscrollview,代码如下: <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:layout_marginRight="20dp"> <LinearLayout android:layout_width="match_parent
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<ImageButton
android:id="@+id/somerandomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/image"
android:layout_marginLeft="20dp"
android:layout_marginTop="140dp" />
<ImageButton
android:id="@+id/somerandomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/image"
android:layout_marginLeft="20dp"
android:layout_marginTop="140dp" />
</LinearLayout>
</HorizontalScrollView>
您需要将布局内容包装起来,例如:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:orientation="horizontal">
<ImageButton
android:id="@+id/somerandomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/image"
android:layout_marginLeft="20dp"
/>
<ImageButton
android:id="@+id/somerandomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/image"
android:layout_marginLeft="20dp"
/>
</LinearLayout>
</HorizontalScrollView>
请发布此版面的所有代码,尽管我可能建议对horizontalSerollView使用特定高度,以保护其不受match\u parent或wrap\u内容的影响,并将RelativeLayout用作子级
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="your_height(not match_parent)"
android:id="@+id/horizontalScrollView"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageButton
android:id="@+id/somerandomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/image"
android:layout_marginLeft="20dp"
android:layout_marginTop="140dp" />
<ImageButton
android:id="@+id/somerandomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/image"
android:layout_marginLeft="20dp"
android:layout_marginTop="140dp" />
</RelativeLayout>
</HorizontalScrollView>
指定水平SerollView的高度。它可以防止匹配父内容或换行内容导致布局崩溃或无结果。由于视图的放置,RelativeLayout最受欢迎。