在滚动视图中居中当前视口上的元素
问题描述:
我想要显示一个始终位于设备物理中心的点,即使我在滚动视图中滚动其周围的内容时也是如此。这是我目前的代码,但是这里缺少点,因为我不知道该把它放在哪里。 scrollposition只能通过代码设置,所以如果居中的元素阻止了滚动视图,那么它就不会成为问题。在滚动视图中居中当前视口上的元素
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:deviceIds="wear_square"
android:gravity="center">
<android.support.v4.widget.NestedScrollView
android:id="@+id/vertsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<HorizontalScrollView
android:id="@+id/horisv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/map" />
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
解决方案:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:deviceIds="wear_square">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="@+id/vertsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<HorizontalScrollView
android:id="@+id/horisv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/map" />
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_my_location_black_24dp" />
</RelativeLayout>
答
把你的点以下布局别的。此外,您应该使用FrameLayout/RelativeLayout而不是LinearLayout作为顶级父容器。
为什么你不能从onDraw()中绘制这个点,而不是从xml构建令人困惑的布局? –
希望你只集中你的形象?请解释一下。 –
最简单的方法是将你的点放在滚动视图之外。在这种情况下,您可以根据需要对齐。 –