【问题标题】:How to change the appearance of a ProgressDialog?如何更改 ProgressDialog 的外观?
【发布时间】:2017-10-04 10:40:59
【问题描述】:

我知道this is possible,但我想知道为什么我的解决方案不起作用。

ProgressDialog dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.custom_text, null, false);
textView.setText(context.getResources().getString(msg));
dialog.setView(textView);

这是自定义文本布局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/messageSpinner"
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorAccent"
android:textStyle="bold">

</TextView>

结果是一个带有预期轮子的窗口,但消息为空。

【问题讨论】:

  • 你改变了 dialog 的视图。现在,您在该自定义 XML 中输入的任何内容都将出现在对话框中,即也放置一个 ProgresBar

标签: android user-interface dialog customization


【解决方案1】:

您正在覆盖对话框视图。现在对话框只显示您的自定义视图。尝试在您的布局中添加进度条

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar 
android:layout_width="30dp"
android:layout_height="30dp"/>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/messageSpinner"
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorAccent"
android:textStyle="bold">

</TextView>
</LinearLayout>

【讨论】:

    【解决方案2】:

    您可以使用对话框库:https://github.com/Samehadar/IOSDialog

    它可以帮助您轻松创建对话框。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      MainActivity.java

      import android.os.Bundle;
      import android.os.Handler;
      import android.app.Activity;
      import android.app.Dialog;
      import android.content.Context;
      import android.view.Gravity;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup.LayoutParams;
      import android.view.WindowManager;
      import android.view.animation.Animation;
      import android.view.animation.LinearInterpolator;
      import android.view.animation.RotateAnimation;
      import android.widget.ImageView;
      import android.widget.LinearLayout;
      
      public class MainActivity extends Activity implements OnClickListener {
      
          private TransparentProgressDialog pd;
          private Handler h;
          private Runnable r;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              h = new Handler();
              pd = new TransparentProgressDialog(this, R.drawable.spinner);
              r =new Runnable() {
                  @Override
                  public void run() {
                      if (pd.isShowing()) {
                          pd.dismiss();
                      }
                  }
              };
              findViewById(R.id.the_button).setOnClickListener(this);
          }
      
          @Override
          public void onClick(View v) {
              pd.show();
              h.postDelayed(r,5000);
          }
      
          @Override
          protected void onDestroy() {
              h.removeCallbacks(r);
              if (pd.isShowing() ) {
                  pd.dismiss();
              }
              super.onDestroy();
          }
      
          private class TransparentProgressDialog extends Dialog {
      
              private ImageView iv;
      
              public TransparentProgressDialog(Context context, int resourceIdOfImage) {
                  super(context, R.style.TransparentProgressDialog);
                  WindowManager.LayoutParams wlmp = getWindow().getAttributes();
                  wlmp.gravity = Gravity.CENTER_HORIZONTAL;
                  getWindow().setAttributes(wlmp);
                  setTitle(null);
                  setCancelable(false);
                  setOnCancelListener(null);
                  LinearLayout layout = new LinearLayout(context);
                  layout.setOrientation(LinearLayout.VERTICAL);
                  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                  iv = new ImageView(context);
                  iv.setImageResource(resourceIdOfImage);
                  layout.addView(iv, params);
                  addContentView(layout, params);
              }
      
              @Override
              public void show() {
                  super.show();
                  RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
                  anim.setInterpolator(new LinearInterpolator());
                  anim.setRepeatCount(Animation.INFINITE);
                  anim.setDuration(3000);
                  iv.setAnimation(anim);
                  iv.startAnimation(anim);
              }
          }
      
      }
      

      activity_main

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:orientation="vertical">
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal"
              android:padding="8dp"
              android:textSize="20sp"
              android:text="Transparent Progress Indicator" />
      
          <Button 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Check it out!"
              android:layout_marginTop="40dp"
              android:layout_gravity="center"
              android:id="@+id/the_button" />"
      
      </LinearLayout>
      

      styles.xml

      <resources>
      
          <!--
              Base application theme, dependent on API level. This theme is replaced
              by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
          -->
          <style name="AppBaseTheme" parent="android:Theme.Light">
              <!--
                  Theme customizations available in newer API levels can go in
                  res/values-vXX/styles.xml, while customizations related to
                  backward-compatibility can go here.
              -->
          </style>
      
          <!-- Application theme. -->
          <style name="AppTheme" parent="AppBaseTheme">
              <!-- All customizations that are NOT specific to a particular API-level can go here. -->
          </style>
      
          <!--  Transparent dialog -->
          <style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
              <item name="android:windowFrame">@null</item>
              <item name="android:windowBackground">@android:color/transparent</item>
              <item name="android:windowIsFloating">true</item>
              <item name="android:windowContentOverlay">@null</item>
              <item name="android:windowTitleStyle">@null</item>
              <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
              <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
              <item name="android:backgroundDimEnabled">true</item>
              <item name="android:background">@android:color/transparent</item>
          </style>
      
      </resources>
      

      drawable/spinner.png

      输出

      【讨论】:

        猜你喜欢
        • 2012-08-20
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多