使对话框宽度达到全屏

时间:2017-11-24 08:00:03

标签: android kotlin alertdialog android-alertdialog

是的,这是多方面的问题,但没有一种方法可行。

我正在尝试将对话框宽度扩展到全屏。

对话框布局:

<?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:id="@+id/constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp">

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

        <TextView
            android:fontFamily="@font/gotham_medium_regular"
            android:textAllCaps="true"
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Login"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/guideline3"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:fontFamily="@font/gotham_light"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="@+id/guideline3"
            app:layout_constraintTop_toBottomOf="@+id/textView6"
            android:id="@+id/textView7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Enter your phone number to proceed" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input"
            android:layout_marginTop="24dp"
            android:layout_width="0dp"
            app:layout_constraintTop_toBottomOf="@+id/textView7"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/guideline4"
            app:layout_constraintStart_toStartOf="@+id/guideline3">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/userPhone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/gotham_light"
                android:hint="PHONE NUMBER"
                android:inputType="phone"
                android:text="+91" />
        </android.support.design.widget.TextInputLayout>



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


        <Button
            android:layout_marginBottom="@dimen/activity_horizontal_margin"
            android:id="@+id/continueButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/button_background"
            android:fontFamily="@font/gotham_light"
            android:text="Continue"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="@+id/guideline4"
            app:layout_constraintStart_toStartOf="@+id/guideline3"
            app:layout_constraintTop_toBottomOf="@+id/text_input" />


    </android.support.constraint.ConstraintLayout>

对话框代码:

 val dialogBuilder = AlertDialog.Builder(ContextThemeWrapper(context,R.style.ThemeDialog))
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val dialogView = inflater.inflate(R.layout.mobile_bottom_sheet_layout, null)

        val phone = dialogView.findViewById<EditText>(R.id.userPhone)
        dialogBuilder.setView(dialogView)
        val alertDialog = dialogBuilder.create()

        val countinueButton = dialogView.findViewById<Button>(R.id.continueButton) as Button

        countinueButton.setOnClickListener {

            if(phone.text.toString().isEmpty()){
                phone.error=getString(R.string.empty)
                phone.requestFocus()
            }else if(phone.text.toString().length!=13){
                phone.error=getString(R.string.phone_length_error)
                phone.requestFocus()
            }else{

                checkIfTrainerExists(phone.text.toString(),this@LoginActivity);
            }

        }

        alertDialog.show()

我尝试过的事情:

  1. https://stackoverflow.com/a/28519059/1640009
  2. https://stackoverflow.com/a/2680670/1640009
  3. https://stackoverflow.com/a/6631310/1640009
  4. 我也尝试了其他解决方案。但没有一个有效。

6 个答案:

答案 0 :(得分:3)

您可以创建自定义FragmentDialog并执行

@Override
public void onResume() {
    // Resize the dialog to full screen.
    if (getDialog() != null && getDialog().getWindow() != null) {
        Window window = getDialog().getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }
    super.onResume();
}

FragmentDialogs的教程:

答案 1 :(得分:2)

查看此示例Full Width Dialog。这可能会对你有帮助。

enter image description here

答案 2 :(得分:1)

试试这个

制作自定义对话框组件

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;

public class CustomDialog extends Dialog {

     public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);// use your layout in place of this.

    }
}

在下面写下您想要的代码。

CustomDialog dialog=new CustomDialog(MainActivity.this, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
                dialog.show();

Screenshot

答案 3 :(得分:0)

尝试使用Dialog代替AlertDialog,以便您可以根据需要创建自定义对话框。

你也可以:

public class CustomDialog extends Dialog{

    public void onCreate(Bundle savedInstance) {

        super.onCreate(savedInstance);

        setContenctView(R.layout.custom_layout);

        // You can manipulate the Dialog using         
        //Window window = getDialog().getWindow();


    }      

}

答案 4 :(得分:0)

对于kotlin中的简单AlertDialog,您可以使用

alertDilog.getWindow().setLayout(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
alertDilog.show()

之后

此处AlertDialogandroid.support.v7.app.AlertDialog

答案 5 :(得分:0)

如果使用ConstraintLayout,它将无法正确匹配匹配约束。为了获取布局参数,请在 onAttachedToWindow 方法上应用它们。

class DialogDownloadFinished(context : Context) : Dialog(context) {

    override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       val dialogView =  DialogDownloadFinishedBinding.inflate(LayoutInflater.from(context),null,false)
       requestWindowFeature(Window.FEATURE_NO_TITLE)
       setContentView(dialogView.root)
    }

    override fun onAttachedToWindow() {
       super.onAttachedToWindow()
       window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
       window.setGravity(Gravity.TOP)
    }
}