【问题标题】:How to make a custom alertdialog fullscreen如何使自定义警报对话框全屏
【发布时间】:2019-02-19 08:38:32
【问题描述】:

我希望制作一个自定义的AlertDialog 全屏,就像一个新的活动屏幕。

试过这些答案还是不行

public void newDialog(final Context c){

    final AlertDialog alertDialog;

    LayoutInflater layoutInflater = LayoutInflater.from(c);
    dialogueView = layoutInflater.inflate(R.layout.layout_dialogue, null);

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(c);
    alertDialogBuilder.setView(dialogueView);

    alertDialog = alertDialogBuilder.create();

    dialogueView.findViewById((R.id.closeBtn)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
        }
    });

    alertDialog.show();
}

layout_dialogue.xml

<?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"
style="@style/full_screen_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/frame">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/closeBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_sky"
    android:clickable="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/ic_delete" />

</android.support.constraint.ConstraintLayout>

style.xml

<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

我也试过了

android.R.style.Theme_Black_NoTitleBar_Fullscreen  

ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;

它们都不适合我。关于如何完成它有什么建议吗?

【问题讨论】:

  • 我认为根据您的要求,您需要使用对话框而不是警报对话框。对话框让您可以使用自己的自定义视图。
  • 是的,我其实可以通过Dialog来实现,但是可以用AlertDialog来实现吗?

标签: android android-alertdialog android-dialog


【解决方案1】:

你可以使用自定义样式让我给你看一个例子

style.xml

<style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <!-- No backgrounds, titles or window float -->
    <item name="android:windowBackground">@color/colorPrimary</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

Activity.java

AlertDialog.Builder builder = new AlertDialog.Builder(this , R.style.DialogTheme)

【讨论】:

  • 感谢您的回答,但我如何才能使用带有特定按钮的自定义布局?
  • 您可以使用自定义布局示例 Dialog dialog = new Dialog(this ,R.style.DialogTheme) dialog.setCustomLayout(R.layout.alert_Layout)
  • 是的,这是一种可行的方法。但是,我正在尝试使用 alertdialog 而不是对话框来做到这一点。这可能吗?
  • 在 name="android:windowIsFloating">true 下效果更好。很好。谢谢。
  • 这会在对话框的顶部和底部显示黑点
【解决方案2】:

您可以使用以下方法。 (它在 Kotlin 中定义,但您也可以为 Java 实现)

class FullscreenDialog: DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_no_internet, container, false)
        return view
    }

    override fun onStart() {
        super.onStart()

        if(dialog != null) {
            val width = ViewGroup.LayoutParams.MATCH_PARENT
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            dialog.window?.setLayout(width, height)
        }
    }

}

将以下内容添加到您的styles.xml

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/appPrimaryColor</item>
    <item name="colorPrimary">@color/appPrimaryColor</item>

    <!-- Set this to true if you want Full Screen without status bar -->
    <item name="android:windowFullscreen">true</item>

    <item name="android:windowIsFloating">false</item>

    <!-- This is important! Don't forget to set window background -->
    <item name="android:windowBackground">@null</item>

</style>   

要显示对话框,请使用以下命令:

val fullScreenDialog = FullscreenDialog()
fullScreenDialog.isCancelable = false
fullScreenDialog.show(supportFragmentManager, "MyTag")

【讨论】:

    【解决方案3】:

    我一直在使用没有边距的全屏对话框

    public class ShareImageEditorDialog extends DialogFragment {
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
            setStyle(DialogFragment.STYLE_NORMAL,
                    android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        }
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.dialog_share_image, container);
    
            return v;
        }
    }
    

    在按钮点击时显示

    new ShareImageEditorDialog().show(((FragmentActivity)activity).getSupportFragmentManager(), "MyTag");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      相关资源
      最近更新 更多