对话框的宽度和高度问题

时间:2018-07-18 08:04:16

标签: java android alertdialog

我使用自定义对话框,但在移动设备和选项卡视图中使用,但是当我在选项卡上进行检查时,设计会更改

我要在所有设备上使用相同的对话框。

请参阅下面的代码。

private void startDialog() {

    final Dialog dialog = new Dialog(activity);
    int width = (int)(getResources().getDisplayMetrics().widthPixels*0.70);
    int height = (int)(getResources().getDisplayMetrics().heightPixels*0.65);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    dialog.setContentView(R.layout.checkout_alert_dialog);
    dialog.show();
    dialog.getWindow().setLayout(width, height);


    btn_cancle = dialog.findViewById(R.id.btn_cancle);
    btn_cancle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity, "Cancel coming soon", Toast.LENGTH_LONG).show();
            dialog.dismiss();
        }
    });

    btn_continue = dialog.findViewById(R.id.btn_continue);
    btn_continue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity, " Ok coming soon", Toast.LENGTH_LONG).show();
            dialog.dismiss();
        }
    });
}

以下是 Dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <ImageView
                android:id="@+id/iv_pointBox"
                android:layout_width="@dimen/_80sdp"
                android:layout_height="@dimen/_80sdp"
                android:src="@drawable/ic_chekout_popup"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/_30sdp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/normal_input_text_size"
                android:text="You've chosen to redeem 865 Points and pay 44.44 AED. The Payment process might take Some times, please do not hit the back button or close the app. Would you like to continue. "
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:layout_below="@+id/iv_pointBox"
                android:layout_marginTop="@dimen/_15sdp"
                />
         <View
             android:layout_width="match_parent"
             android:layout_height="@dimen/_1sdp"
             android:background="@color/view_color"
             android:layout_above="@+id/linerbutton"
             />
         <LinearLayout
             android:id="@+id/linerbutton"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
             android:layout_alignParentBottom="true"
             >
            <Button
                android:id="@+id/btn_cancle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/lable_cancel"
                android:textColor="@color/black"
                android:textSize="@dimen/normal_input_text_size"
                android:textStyle="bold"
                android:background="@null"
                android:gravity="center"
                android:layout_weight="1"
                />

             <View
                 android:layout_width="@dimen/_1sdp"
                 android:layout_height="@dimen/_45sdp"
                 android:background="@color/view_color"
                 />
            <Button
                android:id="@+id/btn_continue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/lable_continue"
                android:textColor="@color/black"
                android:textSize="@dimen/normal_input_text_size"
                android:textStyle="bold"
                android:background="@null"
                android:layout_weight="1"
                android:gravity="center"
                />
         </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

我提到了我的设备的2个屏幕截图:

移动设备屏幕截图:

enter image description here

Tab屏幕截图:

enter image description here

在屏幕截图对话框中看到已更改。 那我该如何解决

预先感谢:

5 个答案:

答案 0 :(得分:1)

仅查看您的layout.xml布局heightwidthmatch_parent即可创建一个空格。

只需用我的xml替换您的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/_250sdp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/white"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <ImageView
                android:id="@+id/iv_pointBox"
                android:layout_width="@dimen/_80sdp"
                android:layout_height="@dimen/_80sdp"
                android:src="@drawable/ic_chekout_popup"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/_30sdp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/normal_input_text_size"
                android:text="You've chosen to redeem 865 Points and pay 44.44 AED. The Payment process might take Some times, please do not hit the back button or close the app. Would you like to continue. "
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:layout_below="@+id/iv_pointBox"
                android:layout_marginTop="@dimen/_15sdp"
                />
         <View
             android:layout_width="match_parent"
             android:layout_height="@dimen/_1sdp"
             android:background="@color/view_color"
             android:layout_marginTop="@dimen/_50sdp"
             />
         <LinearLayout
             android:id="@+id/linerbutton"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
             >
            <Button
                android:id="@+id/btn_cancle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/lable_cancel"
                android:textColor="@color/black"
                android:textSize="@dimen/normal_input_text_size"
                android:textStyle="bold"
                android:background="@null"
                android:gravity="center"
                android:layout_weight="1"
                />

             <View
                 android:layout_width="@dimen/_1sdp"
                 android:layout_height="@dimen/_45sdp"
                 android:background="@color/view_color"
                 />
            <Button
                android:id="@+id/btn_continue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/lable_continue"
                android:textColor="@color/black"
                android:textSize="@dimen/normal_input_text_size"
                android:textStyle="bold"
                android:background="@null"
                android:layout_weight="1"
                android:gravity="center"
                />
         </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

并在java文件中删除以下三行无需在运行时更改heightwidth

int width = (int)(getResources().getDisplayMetrics().widthPixels*0.70);
 int height = (int)(getResources().getDisplayMetrics().heightPixels*0.65);
 dialog.getWindow().setLayout(width, height);

答案 1 :(得分:0)

尝试自定义布局

custom.xml

public void playCycle(){
    int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;
    seek.setProgress(mCurrentPosition);
    if(music.isPlaying()){
        runnable= new Runnable() {
            @Override
            public void run() {
                playCycle();
            }
        };
        handler.postDelayed(runnable, 1000);
    }
}

在Java文件中

sudo /usr/bin/safaridriver --enable

使用打开对话框

<?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" >
<ImageView
android:id="@+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="@+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/imageDialog"/>
<Button
android:id="@+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/textDialog"
android:layout_toRightOf="@+id/imageDialog"
/>
</RelativeLayout>

关闭对话框使用

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

答案 2 :(得分:0)

替换

myAlertDialog.show();

作者

alertDialog.show();

然后

alertDialog.getWindow().setLayout(width,height);  

它将完美运行

完整代码:

  private void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("title");
    myAlertDialog.setMessage("use this");
    AlertDialog alertDialog = myAlertDialog.create();

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = (int) ((int)displaymetrics.widthPixels * 0.9);
    int height = (int) ((int)displaymetrics.heightPixels * 0.7);


    myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            Intent pictureActionIntent = null;

            pictureActionIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(pictureActionIntent, GALLERY_PICTURE);

        }
    });

    myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {

            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 15);

        }
    });
    alertDialog.show();
    alertDialog.getWindow().setLayout(width,height);

}

如果仍然无法正常工作,则可以这样使用:

class DialogView extends DialogFragment {
    Context context;

    public static DialogView newInstance() {
        DialogView f = new DialogView();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme);
        context = this.getActivity();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new Dialog(getActivity(), getTheme()) {
            @Override
            public void onBackPressed() {
                //do your stuff

            }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.button_layout_view, container, false);
        setCancelable(false);
             //  RelativeLayout closeBtn = (RelativeLayout) view.findViewById(R.id.bottom_separator);  get button ids like this- gallery camera etc and you can use click listener on that

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = (int) (metrics.widthPixels * 0.90);
        int height = (int) (metrics.heightPixels * 0.95);
        this.getDialog().getWindow().setLayout(width, height);
        return view;
    }

    public static DisplayMetrics getDeviceMetrics(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getMetrics(metrics);
        return metrics;
    }

}

为此创建带有两个按钮和标题的xml

button_layout_view

然后像这样打电话:

 android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        DialogFragment newFragment = DialogView.newInstance();
       newFragment.show(ft, "");

答案 3 :(得分:0)

我个人更喜欢创建DialogFragment并设置宽度和高度,如下所示:

@Override
public void onResume() {
    super.onResume();
    Window window = getDialog().getWindow();
    int width = getArguments().getInt(BUNDLE_KEY_WIDTH);
    int height = getArguments().getInt(BUNDLE_KEY_HEIGHT);
    window.setLayout(width, height);
}

答案 4 :(得分:0)

在两个设备中,对话框的宽度和高度都很好(您可以通过startDialog()方法进行设置)。 如果您希望平板电脑设备中的ImageView较小,以便在移动设备和平板电脑中使用相同的比例,则应为它们使用不同的dimens文件:

首先,为图像定义尺寸:

内部dimens.xml

<dimen name="dialog_image_size">XXXdp</dimen>

然后,在10英寸平板电脑的 /res/values-sw720dp/dimens.xml 中和 /res/values-sw600dp/dimens.xml 中定义相同的尺寸>用于7英寸平板电脑,并为它们设置较小的dp。

现在,当您在平板电脑上时,将加载不同的尺寸,因此您可以使用较小的尺寸来保持与移动设备相同的比例。