【问题标题】:removing background from progress dialog从进度对话框中删除背景
【发布时间】:2019-04-14 03:14:09
【问题描述】:

我在我的应用程序中使用自定义进度对话框。如下图所示。我要编辑两件事。首先,去除出现在圆圈后面的黑色背景。其次,增加圆圈的音量并更改其颜色。

这就是风格

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#FF4081</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowFrame">@android:color/transparent</item>
    </style>

这是我的代码

dialog = new ProgressDialog(activity.this,R.style.CustomDialog);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.show();

【问题讨论】:

    标签: java android android-layout progressdialog android-styles


    【解决方案1】:

    ProgressDialog 已弃用。您可以使用警报对话框创建自定义进度对话框。

    public static AlertDialog showProgressBar(Context context){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.layout_custom_progress_bar, null);
        builder.setView(view);
        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }
    

    然后您可以实例化该对话框,以便您可以手动关闭它。像这样使用它。

    AlertDialog dialog = showProgressBa(this); // this will automatically show the custom progress bar
    // to dismiss
    dialog.dismiss();
    

    这是进度对话框的自定义布局。

    //layout_custom_progress_bar.xml
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">
    
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:indeterminateDrawable="@drawable/progress_bar"
            android:max="100"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    您的进度条可绘制。您可以通过更改android:centerColor android:endColor android:startColor的值来更改颜色

    // progress_bar
    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="1080" >
    
    <shape
    android:innerRadius="18dp"
    android:shape="ring"
    android:thickness="4dp"
    android:useLevel="false" >
    
    <size
        android:height="48dp"
        android:width="48dp" />
    
    <gradient
        android:centerColor="@color/colorPrimary"
        android:centerY="0.5"
        android:endColor="@color/colorWhite"
        android:startColor="@color/colorPrimary"
        android:type="sweep"
        android:useLevel="false" />
    
    </shape>
    </rotate>
    

    【讨论】:

      【解决方案2】:

      试试这个。

      dialog= new Dialog(mContext);
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialog.setContentView(R.layout.custom_progress_layout);
      dialog.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
      dialog.setCancelable(true);
      dialog.setCanceledOnTouchOutside(true);
      dialog.show();
      

      【讨论】:

        【解决方案3】:

        您可以对该功能使用滑动刷新布局,而不是从对话框中删除背景。

        你可以在java中实现它而不在xml布局中添加它。

        public class Test extends AppCompatActivity{
        
             SwipeRefrehLayout swipeRefresh;
        
             @Override
             protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                swipeRefresh = new SwipeRefreshLayout();
                swipeRefresh.setEnabled(true)
        
                //Set Colour Scheme
                swipeRefreshLayout.setColorSchemeResources(#FF6F00, #880E4F, #000000);
        
                showSwipeRefresh(true);
            }
        
            private void showSwipeRefresh(boolean show){
        
                if (show){
                    swipeRefresh.setRefreshing(true);
                } else {
                    swipeRefresh.setRefreshing(false);
                }
            }
        }
        

        【讨论】:

        • 我知道你好不是你的问题,但你可以试试。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        相关资源
        最近更新 更多