如何设置警报对话框的高度

时间:2018-04-04 08:14:29

标签: android alertdialog

我想在我的应用中设置Alert对话框的高度。由于我在警告对话框中使用了超过10个字段来填充设置为正数且设置为负按钮不可见的数据。现在我想降低警报对话框的高度。我使用 getwindow.setlayout 方法,但它仍处于同一高度。

    package com.clone.tracking.unnamed.mytraining;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.clone.tracking.unnamed.mytraining.Model.Student;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;

public class AdminControl extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    RelativeLayout rootLayout;

    FirebaseDatabase db;
    DatabaseReference students;


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

        db=FirebaseDatabase.getInstance();
        students=db.getReference("Students");


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showStudentRegistrationDialog();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void showStudentRegistrationDialog() {
        final AlertDialog.Builder dialog=new AlertDialog.Builder(this);


        dialog.setTitle("REGISTER");
        dialog.setMessage("Please fill All Fields");
        //AlertDialog alertDialog = builder.create();




        LayoutInflater inflater= LayoutInflater.from(this);
        View student_layout=inflater.inflate(R.layout.layout_details,null);

        final MaterialEditText edtName=student_layout.findViewById(R.id.edtName);
        final MaterialEditText edtRoll=student_layout.findViewById(R.id.edtRoll);
        final MaterialEditText edtSemester=student_layout.findViewById(R.id.edtSemester);
        final MaterialEditText edtFees=student_layout.findViewById(R.id.edtFee);
        final MaterialEditText edtDob=student_layout.findViewById(R.id.edtDOB);
      final MaterialEditText edtEmail=student_layout.findViewById(R.id.edtEmail);
       final MaterialEditText edtPhone=student_layout.findViewById(R.id.edtPhone);
        final MaterialEditText edtSex=student_layout.findViewById(R.id.edtSEX);
        final MaterialEditText edtAddress=student_layout.findViewById(R.id.edtAddress);
        final MaterialEditText edtPercent=student_layout.findViewById(R.id.edtPercentage);
        final MaterialEditText edtcollege=student_layout.findViewById(R.id.edtCollege);
        final MaterialEditText edtDepartment=student_layout.findViewById(R.id.edtDepartment);



        dialog.setView(student_layout);

        //this set the buttons below the pop up dialog

        dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();

                if(TextUtils.isEmpty(edtName.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Name is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtRoll.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Roll Number is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtSemester.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Semester is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtFees.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Fee detail is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtDob.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Date of Birth is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

            if(TextUtils.isEmpty(edtEmail.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Email address is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(edtPhone.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Phone Number is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtSex.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Sex is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(edtAddress.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Address is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtPercent.getText().toString()))
                {
                    Snackbar.make(rootLayout,"OverAll Percentage is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtcollege.getText().toString()))
                {
                    Snackbar.make(rootLayout,"College Name is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(edtDepartment.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Department name is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

               Student student=new Student();
                student.setName(edtName.getText().toString());
                student.setRollno(edtRoll.getText().toString());
                student.setSemester(edtSemester.getText().toString());
                student.setFeedetails(edtFees.getText().toString());
                student.setDob(edtDob.getText().toString());
              student.setEmail(edtEmail.getText().toString());
                student.setPhone(edtPhone.getText().toString());
                student.setSex(edtSex.getText().toString());
                student.setAddress(edtAddress.getText().toString());
                student.setPercentage(edtPercent.getText().toString());
                student.setCollegename(edtcollege.getText().toString());
                student.setDepartment(edtDepartment.getText().toString());

                String email= students.push().getKey();
                students.child(email).setValue(student);
            }
        });


        //this set the cancel button of the dialog
        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog alert = dialog.create();
        alert.getWindow().setLayout(600,400);
        dialog.show();



    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.admin_control, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

3 个答案:

答案 0 :(得分:0)

您可以使用此方法将Alert Dialoge设置为。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400);

您也可以在调用alertDialog.show()方法后使用此方法。

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

lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);

有关详情,请参阅此link

答案 1 :(得分:0)

你可以这样做:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.dialog_change_password);

在您的布局文件中,修复了layout_height。:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="250dp"
              android:layout_gravity="center"
              android:layout_marginLeft="@dimen/activity_horizontal_margin"
              android:layout_marginRight="@dimen/activity_horizontal_margin"
              android:background="@drawable/bg_otp_dialog"
              android:gravity="center"
              android:orientation="vertical">

答案 2 :(得分:0)

尝试以下代码

AlertDialog dialog = builder.create();

                // Finally, display the alert dialog
                dialog.show();

                // Get screen width and height in pixels
                DisplayMetrics displayMetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                // The absolute width of the available display size in pixels.
                int displayWidth = displayMetrics.widthPixels;
                // The absolute height of the available display size in pixels.
                int displayHeight = displayMetrics.heightPixels;

                // Initialize a new window manager layout parameters
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();

                // Copy the alert dialog window attributes to new layout parameter instance
                layoutParams.copyFrom(dialog.getWindow().getAttributes());


                // Set alert dialog width equal to screen width 70%
                 int dialogWindowWidth = (int) (displayWidth * 0.7f);
                // Set alert dialog height equal to screen height 70%
                 int dialogWindowHeight = (int) (displayHeight * 0.7f);

                // Set the width and height for the layout parameters
                // This will bet the width and height of alert dialog
                layoutParams.width = dialogWindowWidth;
                layoutParams.height = dialogWindowHeight;

                // Apply the newly created layout parameters to the alert dialog window
                dialog.getWindow().setAttributes(layoutParams);

下面是我的layout_details.xml文件

     <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardElevation="10dp"
    >
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtName"
            android:hint="Name"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtRoll"
            android:hint="Roll No."
            android:inputType="number"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtSemester"
            android:hint="Semester."
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtFee"
            android:hint="Fee Details"
            android:inputType="number"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtDOB"
            android:hint="DOB(dd/mm/yyyy)"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtSEX"
            android:hint="M/F"
            android:inputType="number"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtAddress"
            android:hint="Address"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtEmail"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtPhone"
            android:hint="Phone number"
            android:inputType="phone"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtPercentage"
            android:hint="Overall percentage"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtCollege"
            android:hint="College Name"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtDepartment"
            android:hint="Department Name"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>


    </LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
相关问题