BringToFront()不适用于ConstraintLayout

时间:2018-07-02 12:21:53

标签: android

我需要将ImageView放在所有布局之上,以显示它与TextView重叠。不幸的是,我从SO所做的任何尝试都没有帮助我。这就是正在发生的事情。

enter image description here

  

main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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" >


    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:padding="10dp"
        android:text="Students"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        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_toBottomOf="@id/txtTitle">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/fraLayout1"
                android:elevation="2dp"
                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">

                <ImageView
                    android:id="@+id/imgKOala"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="-55dp"
                    android:src="@drawable/koala" />

            </FrameLayout>

        </android.support.constraint.ConstraintLayout>

    </FrameLayout>

</android.support.constraint.ConstraintLayout>
  

main.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imgKOala = (ImageView) findViewById(R.id.imgKOala);
    imgKOala.bringToFront();
    imgKOala.invalidate();
    imgKOala.requestLayout();
    ViewCompat.setTranslationZ(imgKOala, 1000);

}

如何正确地使ImageView出现在TextView学生上方? 这只是我真正想要实现的目标。

3 个答案:

答案 0 :(得分:0)

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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" >


    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:padding="10dp"
        android:text="Students"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imgKOala"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="-55dp"
        android:src="@mipmap/ic_launcher">
 </android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

ImageView上设置高程。 2dp似乎有用。

android:elevation="2dp" 

希望有帮助!

答案 2 :(得分:0)

视图的bringToFront和Z顺序都与该视图的父视图相关,因此,如果您在bringToFront上调用imgKOala,则它只是 其父视图fraLayout1。如果您想让imgKOalaTextView重叠,则应该评估FrameLayoutTextView的同级兄弟。

在布局文件中,您的FrameLayout具有以下属性:

app:layout_constraintTop_toBottomOf="@id/txtTitle"

ImageView

android:layout_marginTop="-55dp"

因此您的ImageView不能完全显示,因为它的顶部55dp不在其父视图之外。

尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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" >

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:padding="10dp"
        android:text="Students"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:elevation="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/fraLayout1"
                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">

                <ImageView
                    android:id="@+id/imgKOala"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:src="@mipmap/koala" />

            </FrameLayout>
        </android.support.constraint.ConstraintLayout>
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

只需在与TextView同级的FrameLayout上设置android:elevation=2dp,就可以删除onCreate()方法中的代码。