首页 > 解决方案 > 可缩放的 FrameLayout 获得了绝对宽度和高度,但其中的视图与这些尺寸不匹配

问题描述

好的,大家好。

我对应用程序开发很陌生。

介绍:

在我的应用程序中,我使用RelativeLayout进行活动。在这个布局中,我有可缩放的 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 的比例)显示为显示器允许的(垂直)。因此我需要重叠显示的宽度。

我需要它看起来像什么: 图像

我试过的:

代码在这里:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(height, height);
FrameLayout frame = findViewById(R.id.zoomView);
frame.setLayoutParams(lp);

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


编辑 [21.11.2020]:

因此,正如 Daxi建议的那样,我尝试将ZoomableLayout中的RelativeLayout更改为ConstraintLayout。我还将ZoomableLayout更改为从ConstraintLayout扩展。这几乎解决了我的问题。但是我无法左右滚动。我只能放大。所以我采用了我最后一次尝试的解决方案之一,并用Horizo​​ntalScrollView包装了ZoomableView。一切似乎都很好。但是,我现在无法滚动到ImageView的“开始”或“结束” 。好像我只能在 Horizo​​ntalScrollView 的基本宽度内滚动而不是ImageView宽度。所以我现在需要解决这个问题。

代码在这里:

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

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

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

标签: javaandroidandroid-layout

解决方案


你会考虑使用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 交互


推荐阅读