【问题标题】:Replicate ProgressDialog with AlertDialog使用 AlertDialog 复制 ProgressDialog
【发布时间】:2017-08-10 15:38:16
【问题描述】:

我正在尝试复制 ProgressDialog,因为它已被弃用。 我在 java 中生成了一个新的 AlertDialog,并在前端构建了一个 ProgressBar-round。 爪哇:

AlertDialog alertDialog = new AlertDialog.Builder(SplashScreenActivity.this).create();
alertDialog.setMessage("Logging in...");

布局:

 <ProgressBar
    android:id="@+id/progressBar3"
    style="?android:attr/progressBarStyle"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:indeterminate="true"
    android:visibility="visible"
    tools:layout_editor_absoluteX="284dp"
    tools:layout_editor_absoluteY="343dp" />

但我不明白如何在消息上方的 AlertDialog 中放置进度条。此外,我尝试将进度条设为蓝色,但我没有看到任何类似的属性。谁能给我解释一下(我刚刚开始在 Android Studio 中编程,所以我对我缺乏知识表示歉意)。谢谢大家的时间!

【问题讨论】:

    标签: java android


    【解决方案1】:

    创建一个 MyDialog 类:

     public class MyDialog {
    
        private ProgressDialog dialog;
        private View view;
        Context context;
    public MyDialog(Context context) {
            this.context = context;
            dialog = new ProgressDialog(context, R.style.AlertDialog);
            view=new View(context);
        }
    
        public void ShowProgressDialog(Boolean cancellable) {
            try {
                dialog.show();
                dialog.setCanceledOnTouchOutside(false);
                dialog.setContentView(R.layout.custom_progress_view);
                dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                dialog.setCancelable(cancellable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void CancelProgressDialog() {
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    }
    

    在你的活动中:

    MyDialog mydialog=new MyDialog(this);
    
    mydialog.ShowProgressDialog(false);
    mydialog.CancelProgressDialog();
    

    在样式文件夹中 R.style.AlertDialog:

       <style name="AlertDialog" parent="@android:style/Theme.Material.Light.Dialog.Alert">
        <item name="android:textColor">#000000</item>
    </style>
    

    在您的自定义进度视图 xml 中:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content">
    
        <RelativeLayout
            android:layout_width="220dp"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:padding="@dimen/margin_10"
            android:background="@drawable/header_img">
    
            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:indeterminate="true"
                android:layout_marginTop="@dimen/card_elevation_10"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/white"
                android:layout_gravity="center_vertical|center_horizontal"
                android:maxHeight="32dp"
                android:minHeight="32dp"
                android:minWidth="32dp" />
    
            <TextView
            android:id="@+id/TxtRefPicname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginTop="@dimen/card_elevation_10"
            android:layout_toRightOf="@+id/progressBar"
            android:text="Please Wait..."
                android:layout_margin="@dimen/textsize_20"
            android:textSize="18sp"
            android:textColor="@color/white"
            android:textStyle="normal" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      在 AlertDialogue 的 xml 中使用 ProgressBar;

      LayoutInflater li = LayoutInflater.from(getContext());
      View promptsView = li.inflate(R.layout.prompts_study_level, null);
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
      alertDialogBuilder.setView(promptsView);
      final AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.show();
      final ProgressBar progressBar= (ProgressBar) promptsView.findViewById(R.id.progressBar );
      

      prompts_study_level.xml你可以使用ProgressBar。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-07
        • 1970-01-01
        相关资源
        最近更新 更多