alertDialog 和Dialog 设置只遮挡statusbar 不遮挡navigationbar的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Rodulf/article/details/86548186

1: 第一种是纯的在代码中设置

2:第二种是设置一个背景的图片,然后将图片设置为背景

mainactivity:


public class MainActivity extends AppCompatActivity {

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

    public void alertDialogShowUsingStyle(View view) {
        AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.DialogStyle).create();
        alertDialog.show();

        //alert dialog 必须要show 以后设置这个布局,不然不起作用
        alertDialog.setContentView(R.layout.layout_dialog);
        Window window = alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0,0,0,0);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

    }

    public void dialogShowUsingStyle(View view) {
        Dialog dialog = new Dialog(this,R.style.DialogStyle);

        dialog.setTitle("test");
        dialog.show();


        dialog.setContentView(R.layout.layout_dialog);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0,0,0,0);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

    }

    public void alertDialogShowUsingBackground(View view) {

        AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.BackgroundDialogStyle).create();

        alertDialog.getWindow().setGravity(Gravity.BOTTOM);
        alertDialog.show();


        //alert dialog 必须要show 以后设置这个布局,不然不起作用
        alertDialog.setContentView(R.layout.layout_dialog_background);


        Window window = alertDialog.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,320);
    }


    public void dialogShowUsingBackground(View view) {
        Dialog dialog = new Dialog(this,R.style.BackgroundDialogStyle);

        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.show();

        dialog.setContentView(R.layout.layout_dialog_background);
        Window window = dialog.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,320);

    }
}

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="DialogStyle" parent="android:ThemeOverlay.Material.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:background">@android:color/white</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.6</item>
    </style>

    <style name="BackgroundDialogStyle" parent="android:ThemeOverlay.Material.Dialog">
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:layout_gravity">bottom</item>
        <item name="android:windowBackground">@mipmap/background_1</item>

    </style>

</resources>

layout_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_marginBottom="30dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text"/>


</LinearLayout>

layout_dialog_background.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginBottom="30dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text"/>


</LinearLayout>

猜你喜欢

转载自blog.csdn.net/Rodulf/article/details/86548186