【问题标题】:How to Center ImageView in HorizontalScroll View Android如何在 Horizo​​ntalScroll View Android 中居中 ImageView
【发布时间】:2015-05-11 20:13:27
【问题描述】:

我一直在尝试将 3000 像素 x 1500 像素(大约)的大型壁纸居中,以便在加载时显示图像的中心,然后用户可以向左或向右滚动以查看其余部分。我看过几个例子,它似乎并没有帮助它总是落在图像的左侧!

我只需要另一双眼睛为我指明正确的方向。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black">

   <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="none"
        >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            >

            <ImageView
                android:id="@+id/imgFullscreen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
  </HorizontalScrollView>
</RelativeLayout>

提前致谢!

【问题讨论】:

  • 添加图片后,您是否尝试过在 Horizo​​ntalScrollView 上调用 scrollTo(imageWidth / 2, imageHeight / 2)?

标签: android xml imageview horizontalscrollview


【解决方案1】:

首先您需要将您的线性布局和图像视图宽度设置为“wrap_content” 然后measure 你的 scollView 并得到它的宽度,之后你需要得到 screen size scrollTo 然后滚动到中间

   middle Offset = (scrollView width - screen width ) / 2

【讨论】:

    【解决方案2】:

    首先应该是滚动视图内的布局

     android:orientation="horizontal"
    

    并包装宽度和高度的内容

    最后,您想要以滚动视图为中心启动 Activity。 您可以使用类似

    的方式以编程方式执行此操作
    int centerX = scrollView.getChildAt(0).getWidth()/2;
    int centerY = scrollView.getChildAt(0).getHeight()/2;
    scrollView.scrollTo(centerX, centerY);
    

    例如在 Activity 的 onCreateView 中

    【讨论】:

    • 似乎不起作用..如果它在图像视图上滚动,它会在滚动中移动图像。但我无法让滚动视图移动。
    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    相关资源
    最近更新 更多