使 ViewStub 覆盖 Activity

Making a ViewStub Overlay an Activity

我正在尝试创建一个登录 ViewStub,它将在客户 select 登录按钮时覆盖欢迎 Activity。但是,我 运行 遇到了问题。单击登录时我的存根将打开,但我的欢迎 Activity 上的按钮不会变暗。另外,我是否需要制作我的存根包装内容的背景视图,以便它可以覆盖整个屏幕?或者我应该使用与存根不同的技术来完成我想做的事情吗?谢谢!

我正在尝试完成的屏幕截图

activity_welcome.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"
    android:background="@drawable/gradient_background"
    tools:context=".Activities.Authentication.WelcomeActivity">

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="175dp"
        android:layout_height="75dp"
        android:layout_marginTop="60dp"
        android:contentDescription="@string/logo_image"
        android:scaleType="fitXY"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/skedaddle_services_" />

    <TextView
        android:id="@+id/findQualityServicesTextView"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:text="@string/finding_quality_service_providers_near_you_in_just_seconds"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logoImageView" />

    <TextView
        android:id="@+id/guestOrLogInTextView"
        android:layout_width="314dp"
        android:layout_height="38dp"
        android:layout_marginTop="8dp"
        android:text="@string/continue_as_a_guest_to_look_around_the_app_log_in_or_create_an_account_to_start_requesting_services"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/findQualityServicesTextView" />

    <ViewStub
        android:id="@+id/loginStub"
        android:layout_width="360dp"
        android:layout_height="620dp"
        android:layout="@layout/viewstub_login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="@drawable/first_login_rounded_button"
        android:text="@string/log_in"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/signUpButton"
        app:layout_constraintEnd_toEndOf="@+id/signUpButton"
        app:layout_constraintStart_toStartOf="@+id/signUpButton"
        app:layout_constraintTop_toBottomOf="@+id/guestOrLogInTextView"
        app:layout_constraintVertical_bias="0.13"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/signUpButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/first_signup_rounded_button"
        android:text="@string/sign_up"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/continueAsGuestButton"
        app:layout_constraintEnd_toEndOf="@+id/continueAsGuestButton"
        app:layout_constraintStart_toStartOf="@+id/continueAsGuestButton"
        app:layout_constraintTop_toBottomOf="@+id/loginButton" />

    <Button
        android:id="@+id/continueAsGuestButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="24dp"
        android:background="@android:color/transparent"
        android:text="@string/continue_as_guest"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signUpButton"
        tools:ignore="TextContrastCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>

viewstub_login.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BF090909">

</androidx.constraintlayout.widget.ConstraintLayout>

WelcomeActivty.kt

val loginButton = findViewById<Button>(R.id.loginButton)
loginButton.setOnClickListener {
    val loginStub = findViewById<ViewStub>(R.id.loginStub)
    loginStub.inflate()

}

知道了,我必须设置每个按钮的透明度。

val loginButton = findViewById<Button>(R.id.loginButton)
val signUpButton = findViewById<Button>(R.id.signUpButton)
val guestButton = findViewById<Button>(R.id.continueAsGuestButton)

loginButton.setOnClickListener {
    val loginStub = findViewById<ViewStub>(R.id.loginStub)
    loginStub.inflate()

    loginButton.alpha = 0.25f
    loginButton.isEnabled = false
    signUpButton.alpha = 0.25f
    signUpButton.isEnabled = false
    guestButton.alpha = 0.25f
    guestButton.isEnabled = false
}