Create progress dialog with another layout

我只是一个虾纸丫 提交于 2019-12-10 15:47:51

问题


I want to create a progress dialog which inflates another XML file.

So I tried the following code

public class MyProgressDialog extends AlertDialog {

        public MyProgressDialog(Context context) {
            super(context,R.layout.customprog);

            // TODO Auto-generated constructor stub
        }

    }
MyProgressDialog pdia=new MyProgressDialog(this);

And in asynctask to show this is used pdia.show(); in onPreExecute()

porgspin.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progress1"
android:pivotX="50%"
android:pivotY="50%" />

customprog.xml

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:padding="10dp" >

        <ProgressBar
            android:id="@+id/imageView1"
            android:indeterminateDrawable="@drawable/porgspin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/progress" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="41dp"
            android:layout_toRightOf="@+id/imageView1"
            android:text="Loading..."
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000" />

    </RelativeLayout>

When I run the program I am unable to view the progress bar.

What am I missing here?


回答1:


Second parameter of super(context,R.layout.customprog) accepts a theme resource not a layout. Why don't you inflate the layout instead?

public class MyProgressDialog extends AlertDialog 
{

       @Override
       public void show() {

        super.show();
        setContentView(R.layout.activity_main);
       }

}



回答2:


Instead of passing your custom layout in constructor, override onCreate() method of dialog and use setContentView() method to pass your custom layout..



来源:https://stackoverflow.com/questions/15196879/create-progress-dialog-with-another-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!