Is it possible to alter progress dialog's mess

2019-07-22 19:09发布

I basically want to put message text before progress bar (spinner) in progress dialog in order to look like snackbar.

1条回答
家丑人穷心不美
2楼-- · 2019-07-22 19:52

There might be some easier way to do this that I'm not thinking of at the moment, but you could override the ProgressDialog's onCreate() method, and fiddle with the layout programmatically. For example:

ProgressDialog pd = new ProgressDialog(this) {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ProgressBar progress = (ProgressBar) findViewById(android.R.id.progress);
        LinearLayout bodyLayout = (LinearLayout) progress.getParent();
        TextView messageView = (TextView) bodyLayout.getChildAt(1);

        LinearLayout.LayoutParams llp =
            (LinearLayout.LayoutParams) messageView.getLayoutParams();
        llp.width = 0;
        llp.weight = 1;

        bodyLayout.removeAllViews();
        bodyLayout.addView(messageView, llp);
        bodyLayout.addView(progress);
    }
};

pd.setMessage("Testing...");
pd.show();

I had a quick look through the source versions, and I think this should work for pretty much all of 'em.

查看更多
登录 后发表回答