简体   繁体   English

加载对话框不会 go 透明

[英]Loading Dialog will not go transparent

I have surprisingly been stuck on this one for a little while.令人惊讶的是,我在这个问题上停留了一段时间。

User Story: The user should see a Loading Dialog that can be reused through the application with a transparent background so you only see the progress spinner and the text under the progress spinner.用户故事:用户应该看到一个加载对话框,它可以通过具有透明背景的应用程序重复使用,因此您只能看到进度微调器和进度微调器下的文本。

Currently, I have a DialogFragment that inflates this XML to present itself:目前,我有一个 DialogFragment 膨胀这个 XML 来展示自己:

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
 android:background="@android:color/transparent">

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:alpha="1"
    android:background="@android:color/transparent"
    android:indeterminateDrawable="@drawable/loading_spinner"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.499" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:alpha="1"
    android:background="@android:color/transparent"
    android:text="Loading..."
    android:textColor="#FFFFFF"
    android:textSize="16sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>

I am trying to set the transparency in the background and have had these results:我正在尝试在后台设置透明度并得到以下结果:

  • alpha set changes children elements to transparent as well alpha set 也将子元素更改为透明
  • above XML setting does nothing and shows a white background上面的 XML 设置什么都不做并显示白色背景
  • Setting it programmatically(See below) also does nothing and displays it white.以编程方式设置它(见下文)也不会执行任何操作并将其显示为白色。

LoadingDialog():加载对话框():

   class LoadingDialog(): DialogFragment() {
   private var _binding: FragmentLoadingDialogBinding? = null
   private val binding get() = _binding!!

   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    this.dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
   }

   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState:Bundle?
   ): View? {
      _binding = FragmentLoadingDialogBinding.inflate(inflater, container, false)
      return binding.root
   }

   //Always do this in Dialog to maintain memory management
   override fun onDestroy() {
       super.onDestroy()
       _binding = null
     }
   }

How can I get the above LoadingDialog to present the Loading Progress Spinner and the Text without the white background?如何让上面的 LoadingDialog 呈现加载进度微调器和没有白色背景的文本?

I think you should call onCreate() method before setting the background to transparent after which you should set the view for your dialog.我认为您应该在将背景设置为透明之前调用onCreate()方法,之后您应该为对话框设置视图。 Try this尝试这个

val dialogBinding = // inflate dialog here using dataBinding for example
val customDialog = AlertDialog.Builder(this, 0).create() // works with other dialogs as well

   customDialog.apply {
      window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
      setView(dialogBinding?.root)
    }.show()

I hope this helps you solve your problem.我希望这可以帮助您解决问题。 For more information about Dialogs in Android, please refer to my article on section.io关于Android中的Dialogs的更多信息,请参考我在section.io上的文章

something else, consider removing the transparent background on your root viewGroup as shown below别的,考虑删除你的根 viewGroup 上的透明背景,如下所示

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
 ...
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@android:color/transparent">

edit to编辑为

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
 ...
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM