【问题标题】:Custom Dialog extends Dialog not rendering layout by onCreate is executed自定义对话框扩展对话框不通过 onCreate 呈现布局被执行
【发布时间】:2020-01-07 13:01:20
【问题描述】:

我正在尝试使用以下实现创建自定义对话框。

问题是即使我使用setContentView() 设置了布局,Android 也没有呈现布局。

.show() 被调用的例子:

我搜索了一个可能的解决方案。似乎大多数人使用AlertDialog.Builder,尽管我选择了另一种更简单且可重复使用的路线,即ProgressDialog。另外,this solution 建议将布局资源放在构造函数上——我对此表示怀疑,但自然没有用。

以下代码文件:

源文件ProgressDialog.java

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;

import com.example.android.R;

public class ProgressDialog extends Dialog {

    private ProgressBar progressBar;
    private TextView textView;

    public ProgressDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_dialog);
        progressBar = findViewById(R.id.progressBarDialog);
        progressBar.setProgress(0);
        textView = findViewById(R.id.progressBarText);
    }

    public void setProgress(int progress){
        if (progressBar != null) {
            progressBar.setProgress(progress);
        }
    }

    public void setText(String text){
        if (textView != null) {
            textView.setText(text);
        }
    }
}

并使用 progress_dialog.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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/progressBarText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBarDialog"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBarText" />
</androidx.constraintlayout.widget.ConstraintLayout>

什么原因导致这个问题,为什么Android不渲染对话框内容?

@md-asaduzzaman 的评论更新

使用方法:

ProgressDialog dialog = new ProgressDialog (SomeActivityHere.this);
dialog.show();

// optional
dialog.setProgress(0);
dialog.setText(getString(R.string.someString));

也可以传入一组参数

【问题讨论】:

  • 如何使用这个对话框?
  • @Md.Asaduzzaman 查看更新
  • 将您的
  • @TarunSharma 解决了这个问题。您能否将其发布为解决方案,包括有关其工作原理的详细信息,为什么它不适用于约束布局等
  • 问题出在 width="0dp" 提供 android:layout_width="32dp"

标签: android dialog android-alertdialog customdialog


【解决方案1】:

这是您的layout 的问题。将视图的layout_width0dp 更改为wrap_contentmatch_parent 或根据您的要求的任何固定大小。

android:layout_width="wrap_content"

除此之外您的对话框无法正常工作,因为setProgresssetText 在对话框创建之前执行。所以,改变你的对话框实现如下:

public class ProgressDialog extends Dialog {

    private ProgressBar progressBar;
    private TextView textView;

    private String mText;
    private int mProgress;


    public ProgressDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.progress_dialog);
        progressBar = findViewById(R.id.progressBarDialog);
        progressBar.setProgress(mProgress);

        textView = findViewById(R.id.progressBarText);
        textView.setText(mText);
    }

    public void setProgress(int progress){
        mProgress = progress;

        if (progressBar != null) {
            progressBar.setProgress(mProgress);
        }
    }

    public void setText(String text){
        mText = text;

        if (textView != null) {
            textView.setText(mText);
        }
    }
}

输出:

【讨论】:

    【解决方案2】:

    布局已加载,但如果未设置样式主题,自定义对话框需要固定高度和宽度。快速解决方案是设置高度和宽度。

    <?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:id="@+id/linearLayout"
        android:layout_width="200dp"
        android:layout_height="200dp"
        >
    
      <TextView
          android:id="@+id/progressBarText"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginLeft="16dp"
          android:layout_marginTop="16dp"
          android:layout_marginEnd="16dp"
          android:layout_marginRight="16dp"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          />
    
      <ProgressBar
          android:id="@+id/progressBarDialog"
          style="?android:attr/progressBarStyleHorizontal"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginLeft="16dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="16dp"
          android:layout_marginRight="16dp"
          android:layout_marginBottom="16dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/progressBarText"
          />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    还有:

    ProgressDialog dialog = new ProgressDialog(this);
        dialog.show();
    
        // optional
        dialog.setProgress(0);
        dialog.setText("testing");
    

    【讨论】:

    • 这取决于是否所有屏幕都需要修复设计(尺寸)
    【解决方案3】:

    问题出在 android:layout_width="0dp" 将其更改为特定大小,例如 100dp 或匹配父级

        <TextView
            android:id="@+id/progressBarText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ProgressBar
            android:id="@+id/progressBarDialog"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/progressBarText" />
          </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多