自动在主布局顶部弹出一个对话框

时间:2018-10-31 08:30:35

标签: java android

如何使用android studio在主布局顶部实现自动弹出对话框而无需单击任何按钮 我的案子:
1)登录页面->单击登录
2)登录后->显示带有文本输入的主布局
3)一个弹出对话框(带有一些文本输入,我已经在xml中创建了布局)将自动在主布局的顶部启动。如何实现此目的?

任何参考或方法都非常感谢

enter image description here

3 个答案:

答案 0 :(得分:1)

-设计部分-

<?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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|top"
    android:padding="15dp">

    <android.support.constraint.Guideline
        android:id="@+id/top_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0" />

    <android.support.constraint.Guideline
        android:id="@+id/left_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <android.support.constraint.Guideline
        android:id="@+id/right_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:paddingBottom="20dp"
        app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
        app:layout_constraintRight_toLeftOf="@+id/right_guideline"
        app:layout_constraintTop_toTopOf="@+id/top_guideline">

        <TextView
            android:id="@+id/smsTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:text="@string/resultMessages"
            android:textColor="@color/black"
            android:textSize="@dimen/BiggerTextSize"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/smsTitle"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:layout_weight="1"

            android:gravity="center" />

        <TextView
            android:id="@+id/resultMessages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="22dp"
            android:layout_weight="1"
            android:gravity="center"

            android:textColor="@color/black"
            android:textSize="@dimen/XnormalTextSize" />


    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

View messageDialogView = layoutInflater.inflate(R.layout.error_messages, null);
           TextView resultMessages = messageDialogView.findViewById(R.id.resultMessages);
           ImageView resultImage = messageDialogView.findViewById(R.id.imageView);
            resultImage.setBackground(icon(R.drawable
                    .ic_cancel_black_100dp, R.drawable.ic_cancel_black_140dp, R.drawable
                    .ic_cancel_black_175dp, getResources().getColor(R.color.red)));
           Dialog messageDialog = new Dialog(this);
            messageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            messageDialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable
                    .color_round_white));
            messageDialog.setContentView(messageDialogView);

答案 1 :(得分:0)

您需要像这样使用AlertDialog

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.layout, null);
    // you can use builder.setView(R.layout.layout) instead of the 2 lines above 
    builder.setView(dialogView);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

答案 2 :(得分:0)

在Oncreate中添加自定义对话框

Dialog customDialog=new Dialog(this);
customDialog.setContentView(R.layout.your_layout_name);
Textview yourtextview_name=(TextView)customDialog.findViewById(R.layout.your_textview_id);
//add all views refrence and add two button at bottom of your layout xml (if you want also design your layout)
 customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
 customDialog.show();