Android 自定义v7 AlertDialog样式

作者: javalong | 来源:发表于2017-05-03 20:54 被阅读1184次

在项目中需要弹框,然后设置圆角,其实首先我会想到使用v7 的AlertDialog。可以自定义,使用AlertDialog.Builder构建需要的dialog。
并且支持setView。

如下:

    new AlertDialog.Builder(MainActivity.this)
                        .setView(R.layout.layout_test)
                        .create().show();

layout.test --->乱写的

<?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"
    >

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="dasdnasdn"
        />
</LinearLayout>

确实非常方便。

直接写好了一个弹框。

但是需求上需要背景有圆角,并且跟左右边界距离为30dp。

于是我直接在LinearLayout设置背景发现是无效的。

我首先想到的是设置android:windowBackground

所以直接

<style name="TestAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowBackground">@android:color/holo_red_dark</item>
    </style>

直接设置为红色,然后

 new AlertDialog.Builder(MainActivity.this,R.style.TestAlertDialog)
                        .setView(R.layout.layout_test)
                        .create().show();

会发现如下:

test_1

会发现边上会是一圈黑色。

所以我们需要代码跟入,看看原生style中是如何设置android:windowBackground,然后模仿设置。

AlertDialog默认使用样式 Theme.AppCompat.Light.Dialog.Alert
我们一步步跟入,查看他的样式:

<style name="Theme.AppCompat.Light.Dialog.Alert" parent="Base.Theme.AppCompat.Light.Dialog.Alert"/>
<style name="Base.Theme.AppCompat.Light.Dialog.Alert">
        <item name="windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
        <item name="windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>
    </style>
<style name="Base.Theme.AppCompat.Light.Dialog" parent="Base.V7.Theme.AppCompat.Light.Dialog"/>
<style name="Base.V7.Theme.AppCompat.Light.Dialog" parent="Base.Theme.AppCompat.Light">
        <item name="android:colorBackground">?attr/colorBackgroundFloating</item>
        <item name="android:colorBackgroundCacheHint">@null</item>

        <item name="android:windowFrame">@null</item>
        <item name="android:windowTitleStyle">@style/RtlOverlay.DialogWindowTitle.AppCompat</item>
        <item name="android:windowTitleBackgroundStyle">@style/Base.DialogWindowTitleBackground.AppCompat</item>
        <item name="android:windowBackground">@drawable/abc_dialog_material_background</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>

        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>

        <item name="listPreferredItemPaddingLeft">24dip</item>
        <item name="listPreferredItemPaddingRight">24dip</item>

        <item name="android:listDivider">@null</item>
    </style>

这里我们需要了解一个知识点
<style name="Base.Theme.AppCompat.Light.Dialog.Alert"> 没有写parent,但是由于名字,所以该style继承于<style name="Base.Theme.AppCompat.Light.Dialog">

这里我就不过多介绍了,大家有兴趣,可以搜索下android style继承。

最终我们在Base.V7.Theme.AppCompat.Light.Dialog这个样式中找到
android:windowBackground@drawable/abc_dialog_material_background

跟入

<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="16dp"
       android:insetTop="16dp"
       android:insetRight="16dp"
       android:insetBottom="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@android:color/white" />
    </shape>
</inset>

没错,这就是AlertDialog默认样式,所以我们可以复制这个文件,然后对其进行修改,如我的需求

  1. 背景为红色
  2. 圆角20dp
  3. 左右距离30dp

就可以修改
test_dialog.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetLeft="30dp"
       android:insetTop="16dp"
       android:insetRight="30dp"
       android:insetBottom="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="20dp" />
        <solid android:color="@android:color/holo_red_dark" />
    </shape>
</inset>

修改样式

<style name="TestAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
       <item name="android:windowBackground">@drawable/test_dialog</item>
    </style>

在原来的AlertDialog样式基础之上修改android:windowBackground
然后代码中设置样式。

new AlertDialog.Builder(MainActivity.this,R.style.TestAlertDialog)
                        .setView(R.layout.layout_test)
                        .create().show();

运行如下:

test2

这样就达到了想要的效果。

当然有人说还不如自定义view,想怎么搞怎么搞。还是要看自己的需求,如果只是简单的设置圆角,修改边距的话,还是这样直接修改样式会方便点。

demo很low,大家将就看。

相关文章

网友评论

本文标题:Android 自定义v7 AlertDialog样式

本文链接:https://www.haomeiwen.com/subject/tuyltxtx.html