2017-03-08 50 views
0

在任何设备上我们都有progressDialog里面progressBar的不同位置。所以我需要定制它。如何创建自定义progressDialog

当我尝试这样的:

public class CustomProgressDialog extends ProgressDialog { 

    public CustomProgressDialog(Context context) { 
     super(context); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.progress_fragment_dialog); 
    } 
    } 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:background="@color/colorPrimary" 
     android:layout_height="match_parent" 
     android:id="@+id/progressBar" /> 

</LinearLayout> 

这个进度对话框仅显示进度条没有对话。但我也需要对话。

+0

http://islandofatlas.net/2014/03/29/android-custom-progress-dialog.html尝试此链接 –

回答

2

创建带有进度条的自定义对话框,以使其与不同设备保持一致。

final Dialog dialog = new Dialog(context); 
dialog.setContentView(R.layout.custom); 
dialog.setTitle("Title..."); 

// set the custom dialog components - title, ProgressBar and button 
TextView text = (TextView) dialog.findViewById(R.id.text); 
text.setText("COUSTOM PROGRESS TITLE"); 
ProgressBar prog = (ProgressBar) dialog.findViewById(R.id.myprogress); 
prog.setVisibilty(VISIBLE); 

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
    // if button is clicked, close the custom dialog 
    dialogButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     dialog.dismiss(); 
    } 
}); 

dialog.show(); 

布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ProgressBar 
     android:id="@+id/myprogress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:layout_marginRight="5dp" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     android:layout_toRightOf="@+id/image"/>/> 

    <Button 
     android:id="@+id/dialogButtonOK" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text=" Ok " 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_below="@+id/myprogress" 
     /> 

</RelativeLayout> 
+0

你的意思,我不需要创建扩展Dialog的类?但是每次我想使用进度对话框时,都会有这么多代码! –

+0

如果你知道如何解决它,你也可以做到这一点,否则你可以这样做与此相同。 –