将带有高度的视图放在MaterialCardView和ConstraintLayout上

时间:2018-09-26 16:28:19

标签: android android-constraintlayout

在包含MaterialCardView的布局中,我要添加app:cardElevation="10dp"

enter image description here

但是,当我应用cardElevation时,我的按钮就会混乱。

enter image description here

我尝试了其他问题的许多建议,例如为按钮添加大量仰角或更改Z顺序,但这是行不通的。

    ViewCompat.setElevation(button, 1000.0f)
    ViewCompat.setTranslationZ(button, 1000.0f)
    button.bringToFront()
    login_card.invalidate()

有什么想法吗?

Complete Layout Xml 布局XML(已缩短):

<com.google.android.material.card.MaterialCardView
    android:id="@+id/login_card"
    android:layout_width="match_parent"
    app:cardElevation="10dp i'm breaking the layout-----------"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.textfield.TextInputLayout
        ...
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
        ...
        </com.google.android.material.textfield.TextInputLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/login_button_login"
    android:layout_width="@dimen/login_button_width"
    android:layout_height="wrap_content"
    app:elevation="1000dp"
    android:text="@string/login_button_login"
    app:layout_constraintBottom_toBottomOf="@id/login_card"
    app:layout_constraintEnd_toEndOf="@+id/login_card"
    app:layout_constraintStart_toStartOf="@+id/login_card"
    app:layout_constraintTop_toBottomOf="@+id/login_card"
    app:layout_constraintVertical_bias="1" />

2 个答案:

答案 0 :(得分:0)

这种骇人听闻的方式对我有用。
只需用MaterialCardViewFrameLayout包裹起来即可。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/login_card"
        android:layout_width="match_parent"
        app:cardElevation="10dp i'm breaking the layout-----------"
        android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.textfield.TextInputLayout
                ...
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                ...
            </com.google.android.material.textfield.TextInputLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</FrameLayout>

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/login_button_login"
    android:layout_width="@dimen/login_button_width"
    android:layout_height="wrap_content"
    app:elevation="1000dp"
    android:text="@string/login_button_login"
    app:layout_constraintBottom_toBottomOf="@id/login_card"
    app:layout_constraintEnd_toEndOf="@+id/login_card"
    app:layout_constraintStart_toStartOf="@+id/login_card"
    app:layout_constraintTop_toBottomOf="@+id/login_card"
    app:layout_constraintVertical_bias="1" />

答案 1 :(得分:0)

我只需要使用app:cardElevation="0dp",以便可以在其上放置其他视图。我将panel用作其他按钮和文本的背景。

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/panel"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="16dp"
        app:cardElevation="0dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/autogrow"
        app:layout_constraintVertical_chainStyle="packed"

        />