【问题标题】:How can I remove my layout background so I can see the activity behind it?如何删除我的布局背景以便我可以看到它背后的活动?
【发布时间】:2021-05-31 21:57:13
【问题描述】:

我正在使用ProgressBarTextView 实现自定义加载屏幕,以向用户显示我正在加载活动中的数据。目前,由ProgressBarTextView 组成的布局显示为灰色背景,我想将其删除。我只想显示没有任何背景的加载文本和进度条。我怎样才能做到这一点?

布局文件

<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"
    
    >

    <ProgressBar
        android:id="@+id/progressBarloading"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBarloading"
        app:layout_constraintVertical_bias="0.074"
        android:textSize="18sp"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

目前看起来是这样的,如何去除灰色背景?

【问题讨论】:

  • 在主布局中使用 opacity 属性
  • 没有这样的不透明度选项
  • 当我使用android:background="#00FFFFFF" 时,它给了我同样的结果
  • 其次,我不明白黑色背景是从哪里来的,因为我没有在我的布局文件中指定

标签: android android-layout android-alertdialog android-progressbar


【解决方案1】:

您好,如果您想制作透明对话框,您必须在布局中将背景更改为透明,并且在您的对话框中您需要使用dialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);

这是我的代码

布局.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="@android:color/transparent"
    >

    <ProgressBar
        android:id="@+id/progressBarloading"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBarloading"
        app:layout_constraintVertical_bias="0.074" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是对话框中的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog, null, false);
        builder.setView(view);
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);

【讨论】:

  • 如何使用 LinearLayout 来做到这一点?就像我的按钮后面有一个线性布局,我希望按钮后面的背景是透明的。但是,当我为按钮指定背景时,它也不会显示按钮的背景
  • 在对话框内的相同布局中?
  • 不,这与对话框无关。我在这里发布了问题 - stackoverflow.com/questions/66440517/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2018-02-05
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多