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


好的,大家好。

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

简介:

在我的应用程序中,我有RelativeLayout的活动。在此布局中,我有可缩放FrameLayout。在这个布局中,我必须只有一个布局。在我的情况下,这是另一个RelativeLayout。最后,在这个布局中,我有许多ImageViews。此活动的目的是通过开关显示地图层(可绘制(。

此处编码:

<?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的比例(尽可能大的显示(垂直(。因此,我需要重叠显示器的宽度。

我需要它的外观:图像

我尝试过的:

  • 我认为FrameLayout可以做到这一点。所以我尝试通过编程设置它的宽度高度,所以它适合屏幕高度相同的宽度,因为前面提到的1:1的比例。如果我尝试记录这些维度,似乎一切都设置好了。因为ImageViews及其父RelativeLayout中的match_parent[/em>属性,因此它应该执行所有其他操作,并拉伸这些视图以填充FrameLayout。但一切都只是仅适合屏幕宽度。此处的结果图像:图像

此处编码:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(height, height);
FrameLayout frame = findViewById(R.id.zoomView);
frame.setLayoutParams(lp);
  • 我对普通FrameLayout尝试了相同的方法,但结果相同结果
  • 我还尝试与交换FrameLayout水平滚动视图和自定义可缩放图像视图。这或多或少效果不错,但问题是当我缩放地图时。水平滚动通过缩放的地图打断每一次移动。因此我尝试创建自定义HorizontalScrollView,方法为禁用滚动。即使我从ImageView捕获缩放状态并将滚动设置为notEnabled,它仍然不起作用

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


编辑[22.11.200]:

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

此处编码:

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

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

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

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

最新更新