Как сделать так, чтобы диалог оповещения занимал 90% размера экрана? - PullRequest
267 голосов
/ 21 февраля 2010

Я могу нормально создавать и отображать пользовательское диалоговое окно с предупреждением, но даже при этом у меня есть android:layout_width/height="fill_parent" в диалоговом окне xml, оно не больше, чем его содержимое.

Мне нужен диалог, который заполняет весь экран, за исключением, возможно, отступа в 20 пикселей. Затем изображение, являющееся частью диалога, автоматически растянется до полного размера диалога с помощью fill_parent.

Ответы [ 24 ]

0 голосов
/ 28 марта 2018

вам нужно использовать стиль @ style.xml, например CustomDialog, для отображения настраиваемого диалогового окна.

<style name="CustomDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/colorWhite</item>
        <item name="android:editTextColor">@color/colorBlack</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    </style>

и используйте этот стиль в Activity.java следующим образом

Dialog dialog= new Dialog(Activity.this, R.style.CustomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.custom_dialog);

и ваш custom_dialog.xml должен находиться внутри вашей директории макета

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="20dp"
        android:id="@+id/tittle_text_view"
        android:textColor="@color/colorBlack"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp">

        <EditText
            android:id="@+id/edit_text_first"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:inputType="number" />

        <TextView
            android:id="@+id/text_view_first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>

        <EditText
            android:id="@+id/edit_text_second"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:layout_marginLeft="5dp"
            android:inputType="number" />

        <TextView
            android:id="@+id/text_view_second"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>

    </LinearLayout>

</LinearLayout>
0 голосов
/ 05 марта 2018
    final AlertDialog alertDialog;

    LayoutInflater li = LayoutInflater.from(mActivity);
    final View promptsView = li.inflate(R.layout.layout_dialog_select_time, null);

    RecyclerView recyclerViewTime;
    RippleButton buttonDone;

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
    alertDialogBuilder.setView(promptsView);

    // create alert dialog
    alertDialog = alertDialogBuilder.create();

    /**
     * setting up window design
     */
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


    alertDialog.show();

    DisplayMetrics metrics = new DisplayMetrics(); //get metrics of screen
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = (int) (metrics.heightPixels * 0.9); //set height to 90% of total
    int width = (int) (metrics.widthPixels * 0.9); //set width to 90% of total

    alertDialog.getWindow().setLayout(width, height); //set layout
    recyclerViewTime = promptsView.findViewById(R.id.recyclerViewTime);


    DialogSelectTimeAdapter dialogSelectTimeAdapter = new DialogSelectTimeAdapter(this);
    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerViewTime.setLayoutManager(linearLayoutManager);
    recyclerViewTime.setAdapter(dialogSelectTimeAdapter);

    buttonDone = promptsView.findViewById(R.id.buttonDone);
    buttonDone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            alertDialog.dismiss();

        }
    });
0 голосов
/ 19 июля 2017
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
0 голосов
/ 19 ноября 2014

Вот короткий ответ, который сработал для меня (протестировано на API 8 и API 19).

Dialog mDialog;
View   mDialogView;
...
// Get height
int height = mDialog.getWindow()
.getWindowManager().getDefaultDisplay()
.getHeight();

// Set your desired padding (here 90%)
int padding = height - (int)(height*0.9f);

// Apply it to the Dialog
mDialogView.setPadding(
// padding left
0,
// padding top (90%)
padding, 
// padding right
0, 
// padding bottom (90%)
padding);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...