set width of custom dialog to wrap content android

旧城冷巷雨未停 提交于 2021-02-18 21:11:08

问题


I want to set the width of custom dialog to wrap content but always it fill all the width of the screen

I have tested this

android.view.WindowManager.LayoutParams params = mydialog.getWindow().getAttributes();
params.width = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
mydialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

and that

mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

回答1:


I also having a hard time in solving this problem and finally got a better way in solving this.

Here is the workaround. In your code

mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

This is not working because the default layout for ProgressDialog has match_parent with margins left and right. To resize the ProgressDialog you need a Custom View like below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="@android:drawable/dialog_holo_light_frame"
              android:layout_gravity="center_horizontal"
              android:id="@+id/container"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    <ProgressBar
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/progressBar"/>

    <TextView
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="30dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:gravity="center"
            android:id="@+id/message"/>
</LinearLayout>

and inflate it after showing the ProgressDialog. After inflating the custom view, get the width of the custom view by using the GlobalLayoutListener and set the layout width.

mDialog.show();

final Window window = mDialog.getWindow();
window.setContentView(R.layout.custom_progress_dialog); // this is the above code

final LinearLayout dialogContainer = (LinearLayout) window.findViewById(R.id.container);
TextView message = (TextView) window.findViewById(R.id.message);
message.setText("Loading . . .");
dialogContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int width = dialogContainer.getWidth();
        window.setLayout(width, WindowManager.LayoutParams.WRAP_CONTENT);
        dialogContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});



回答2:


Try below code to have a dialog with wrap content. (In dialogs layout xml use layout_width= WRAP_PATENT and height as WRAP_CONTENT)

 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();


lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.show();
        dialog.getWindow().setAttributes(lp);



回答3:


I know this question is old, but if someone doesn't want to add WindowManager.LayoutParams then just set theme of the dialog to android.R.style.Theme_Holo_Light_Dialog_NoActionBar this will make the dialog to wrap it's width to its contents.

Example:

AlertDialog alert = new AlertDialog.Builder(context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar).create();

Make sure that the layout you are inflating is set to wrap_content for both height and width.




回答4:


Did you try using this

mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

after writing mydialog.show()?

That should work.




回答5:


set style to custom dialog with following attribute:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">

    <item name="android:windowIsFloating">false</item>

</style>


来源:https://stackoverflow.com/questions/21258876/set-width-of-custom-dialog-to-wrap-content-android

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