【问题标题】:Zoomable FrameLayout got absolute width and height but Views in it do not match these dimensions可缩放 FrameLayout 具有绝对宽度和高度,但其中的视图与这些尺寸不匹配
【发布时间】:2020-11-22 02:21:45
【问题描述】:

好的,大家好。

我是应用程序开发的新手。

简介:

在我的应用中,我有 RelativeLayout 的活动。在这个布局中,我有 zoomable FrameLayout。在这个布局中,我必须只有一个布局。就我而言,它是另一个 RelativeLayout。最后在这个布局中,我有许多 ImageView。此活动的目的是通过开关显示地图层(drawables)。

代码在这里:

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorDarkBlueGrey"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

      <com.example.cotwcompanion.ZoomableView
        android:id="@+id/zoomView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

          <RelativeLayout
            android:id="@+id/mapLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

              <ImageView
                android:id="@+id/mapBCG"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:contentDescription="@string/map_name"/>
            
              <!-- Here I generate other ImageViews with 'same' attributes -->

          </RelativeLayout>

       </com.example.cotwcompanion.ZoomableView> 

    <!-- Here are other layouts -->

</RelativeLayout>

问题:

我的问题或多或少是视觉问题。我想将这些层(1:1 的比例)显示为显示器允许的(垂直)。因此我需要重叠显示的宽度。

我需要它的样子: IMAGE

我尝试过的:

  • 我认为 FrameLayout 会这样做。所以我尝试设置它的 widthheight 以编程方式,因此它适合屏幕高度并具有 相同的宽度,因为前面提到的 1:1 比例。如果我尝试log 这些维度,似乎一切都已设置好。因为 ImageView 及其父 RelativeLayout 中的 ma​​tch_parent 属性em>,因此它应该做其他所有事情并拉伸这些视图以填充 FrameLayout。但一切只是 只适合屏幕宽度。 此处的结果图片: IMAGE

代码在这里:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(height, height);
FrameLayout frame = findViewById(R.id.zoomView);
frame.setLayoutParams(lp);
  • 我尝试了与普通 FrameLayout 相同的方法,但结果相同 结果。
  • 我也尝试将 FrameLayoutHorizo​​ntalScrollView 和自定义zoomable ImageView。这或多或少地工作得很好,但问题是当我缩放地图时。 水平滚动会破坏缩放地图的每一个动作。为此我 尝试使用方法创建 custom Horizo​​ntalScrollView 禁用滚动。即使我从 ImageView 捕捉到 缩放状态 并将滚动设置为notEnabled,它仍然不起作用。

然后就可以了。就像我说的缩放方法和其他所有方法一样。我只需要以某种方式拥有比显示允许的更大的视图。


编辑 [21.11.2020]:

所以作为大喜suggested我尝试将ZoomableLayout中的RelativeLayout改为约束布局。我还将 ZoomableLayoutConstraintLayout 更改为 extend。这几乎解决了我的问题。但是我无法左右滚动。我只能放大。所以我采用了我上次尝试的解决方案之一,并用 Horizo​​ntalScrollView 包裹了 ZoomableView。一切似乎都很好。但是,我现在无法滚动到 ImageView 的“开始”或“结束”。似乎我只能在 Horizo​​ntalScrollViewbase width 内滚动,而不能在 width 内滚动>图像视图。所以我现在需要解决这个问题。

代码在这里:

<RelativeLayout>
  <HorizontalScrollView>
    <ZoomableView extends ConstraintLayout>
      <ConstraintLayout>
        <ImageView/>
        .
        .
      </ConstraintLayout>
    </ZoomableView>
  </HorizontalScrollView>
  .
  .
</RelativeLayout>

如果这与 stackoverflow 上已经描述的其他问题重复,我感到非常抱歉,但我现在已经搜索了两天的解决方案,但仍然没有找到任何解决方案。也许我是不好的发现者。如果是这种情况,请随时告诉我。如果需要,我会删除它。

如果会有一些非常好的答案和解决方案,我将不胜感激。谢谢。

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    你会考虑使用ConstraintLayouts吗?如果是的话,你可以这样使用它

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/mapLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:id="@+id/mapBCG"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:contentDescription="@string/map_name"/>
    
            <!-- Here I generate other ImageViews with 'same' attributes -->
            
        </androidx.constraintlayout.widget.ConstraintLayout>
    

    但是,我不确定这将如何与您的 ZoomableView 交互

    【讨论】:

    • 感谢大喜的建议。我试图将RelativeLayout 更改为ConstraintLayout。我也将 ZoomableView 更改为从 ConstraintLayout 扩展。我用 Horizo​​ntalScrollView 包装了所有东西。现在我无法滚动到 ImageView 的开头或结尾,可能是因为 Horizo​​ntalScrollView 的宽度。一切都在 EDIT 部分进行了解释。
    猜你喜欢
    • 2012-06-29
    • 2012-08-18
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2020-01-16
    • 2018-11-08
    • 1970-01-01
    • 2017-05-25
    相关资源
    最近更新 更多