我的对话框显示不正确

时间:2019-01-07 06:44:01

标签: android android-layout android-activity android-theme android-dialog

我已经创建了一个自定义对话框布局,我试图将对话框显示为活动,而不是在警报对话框中放大视图。但是对话框未按要求显示。请看看。

自定义对话框布局

Custom Dialog Layout

对话框活动如下所示

The dialog activity is being shown like below

您可以看一下下面的代码

MainActivity

(?i)(?<!a|e|i|o|u)(\w)led(?<!paralleled)

ImageDialogActivity

public class MainActivity extends AppCompatActivity {

    private static final int PROFILE_PHOTO = 1;

    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.image);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showImageOptionsDialog();
            }
        });
    }

    private void showImageOptionsDialog() {
        Intent imageDialogIntent = new Intent(this, ImageDialogActivity.class);
        startActivityForResult(imageDialogIntent, PROFILE_PHOTO);
    }
}

activity_image_dialog.xml

public class ImageDialogActivity extends AppCompatActivity {

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

清单

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/sixteen"
    android:clipToPadding="false"
    tools:context=".ImageDialogActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="@dimen/eight"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/label_profile_pic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="24dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:text="@string/label_profile_photo"
                android:textColor="@color/primary_text"
                android:textSize="@dimen/dialog_label"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <View
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="@dimen/divider_height"
                android:layout_marginTop="40dp"
                android:background="@color/secondary_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/option_gallery" />

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/cancel_button_bg"
                android:text="@string/action_cancel"
                android:textAllCaps="false"
                android:textAppearance="@style/TextAppearance.AppCompat.Small"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/divider" />

            <TextView
                android:id="@+id/option_gallery"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:drawableTop="@mipmap/ic_pick_img"
                android:drawablePadding="@dimen/eight"
                android:gravity="center"
                android:text="@string/option_gallery"
                android:textColor="@color/primary_text"
                app:layout_constraintEnd_toStartOf="@+id/option_remove_pic"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/label_profile_pic" />

            <TextView
                android:id="@+id/option_remove_pic"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:drawableTop="@mipmap/ic_remove_pic"
                android:drawablePadding="@dimen/eight"
                android:gravity="center"
                android:text="@string/option_remove_photo"
                android:textColor="@color/primary_text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/option_gallery"
                app:layout_constraintTop_toTopOf="@+id/option_gallery"
                app:layout_constraintVertical_bias="0.0" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

我无法找出问题所在。请帮忙。问候!

1 个答案:

答案 0 :(得分:2)

对我有用

DisplayMetrics metrics = getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        dialog.getWindow().setLayout((6 * width) / 7, LinearLayout.LayoutParams.WRAP_CONTENT);