简体   繁体   English

如果 ImageView 包含在不覆盖整个屏幕的 ViewPager 的片段中,是否可以使 ImageView 覆盖整个屏幕?

[英]Is it possible to make an ImageView cover the whole screen, if it's contained in a fragment in a ViewPager that doesn't cover the whole screen?

I am using a ViewPager in an activity layout which is a ConstraintLayout .我在一个ConstraintLayout的活动布局中使用ViewPager This ViewPager is constrained in order to be displayed below a sibling widget.ViewPager受到约束,以便显示在同级小部件下方。 This ViewPager , so, doesn't cover the whole screen.因此,这个ViewPager并没有覆盖整个屏幕。

However, in the fragments of this ViewPager , there is an ImageView that must be displayed covering the whole screen.但是,在这个ViewPager的片段中,有一个ImageView必须覆盖整个屏幕显示。 This ImageView is unique per fragment of this ViewPager (implying that each ImageView must actually be contained in each ViewPager 's fragments).这个ImageView这个ViewPager的每个片段都是唯一的(这意味着每个ImageView实际上必须包含在每个ViewPager的片段中)。

The problem is: since this ImageView is within the fragments of the ViewPager that doesn't cover the whole screen, then it's impossible to use something like match_parent for this ImageView , since it will match the ViewPager which, obviously, doesn't cover all the screen (since it's shown below a sibling widget).问题是:由于这个ImageView是在ViewPager的片段中,没有覆盖整个屏幕,所以不可能对这个ImageView使用类似match_parent的东西,因为它会匹配ViewPager ,显然,它不能覆盖所有屏幕(因为它显示在同级小部件下方)。

What could I do to make the ImageView fill the whole screen?我该怎么做才能让ImageView填满整个屏幕?

When you show the imageView fragment in view pager you can change your constraints programmatically.当您在视图寻呼机中显示 imageView 片段时,您可以以编程方式更改您的约束。 Here there is simple example for this这里有一个简单的例子

ConstraintLayout: change constraints programmatically ConstraintLayout:以编程方式更改约束

fragment.xml片段.xml

<androidx.constraintlayout.widget.ConstraintLayout 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">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPagerImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

viewpger_item.xml viewpger_item.xml

<androidx.constraintlayout.widget.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imgPool"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM