使 ViewStub 覆盖 Activity

Posted

技术标签:

【中文标题】使 ViewStub 覆盖 Activity【英文标题】:Making a ViewStub Overlay an Activity 【发布时间】:2021-12-01 10:35:24 【问题描述】:

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

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

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

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
    android:layout_
    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()


【问题讨论】:

【参考方案1】:

知道了,我必须设置每个按钮的 alpha。

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

【讨论】:

以上是关于使 ViewStub 覆盖 Activity的主要内容,如果未能解决你的问题,请参考以下文章

Android中文API-ViewStub

Android--UI之ViewStub

Android - ViewStub

从源代码角度分析ViewStub 疑问与原理

ViewStub的使用

[Android]Android布局优化之 ViewStub