如何在android中的警告对话框中添加阴影效果

时间:2017-01-18 09:33:11

标签: android

我想在警告对话框中添加阴影效果。 i want this type of shadow effect in my dialog box 这里我首先发布3个文件是style.xml,第二个是theme.java,第三个文件是demo_bg.xml file.i已经尝试过这样

Style.xml 
 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/bg_color</item>
        <item name="android:textColorPrimary">@color/colorBlack</item>
        <item name="android:background">@drawable/demo_bg</item>

    </style>

theme.java

final AlertDialog.Builder builder =
                new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
        builder.setMessage(R.string.wsdialogdata);
        builder.setPositiveButton("START", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent ii = new Intent(WelcomeFromActivity.this, Question1Activity.class);
                startActivity(ii);
            }
        });
        builder.show();



demo_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <!-- SHADOW LAYER -->
            <item android:left="2dp" android:top="2dp">
                <shape>
                    <solid android:color="#66000000" />
                    <corners android:radius="35dip" />
                </shape>
            </item>
            <!-- CONTENT LAYER -->
            <item android:bottom="4dp" android:right="4dp">
                <shape>
                    <solid android:color="@color/btn_fb" />
                    <corners android:radius="40dip" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

3 个答案:

答案 0 :(得分:1)

我在

下面创建了自定义阴影文件(shadow.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"> <!-- this shape is used as shadow -->
            <padding android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp"/>
            <solid android:color="#44000000"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle"> <!-- this is for dialog frame -->
            <corners android:radius="5dp"/>
            <stroke android:color="#ff272727" android:width="2dp" />
            <gradient android:angle="90"
                android:startColor="#ffa7a7a7"
                android:centerColor="#ff6a6a6a"
                android:endColor="#ffa7a7a7"
                android:type="linear"/>
        </shape>
    </item>
</layer-list>

在下一步中,您应该按照以下内容更改对话框主题:

<style name="dialog_theme">
     <item name="android:windowBackground">@drawable/shadow</item>
 </style>

现在你完成了,只需创建一个Dialog类的新实例并将此主题应用于它(在Dialog构造函数中):

Dialog dialog = new Dialog(this, R.style.dialog_theme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mdialog);
dialog.show();

答案 1 :(得分:1)

我知道我来不及。然而,它可能会帮助那些需要帮助并希望以简单方式实现这一目标的人。 只需将其添加到xml中的父布局。

android:background="@android:drawable/dialog_holo_light_frame"

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:drawable/dialog_holo_light_frame"
          android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:drawablePadding="10dp"
    android:drawableStart="@drawable/filter"
    android:gravity="center"
    android:text="Filter"
    android:textColor="@color/grey"
    android:textSize="18sp" />

</LinearLayout>

所以这里LinearLayout是你的父母。所以补充一下,

android:background="@android:drawable/dialog_holo_light_frame"

会这样做。

希望它有所帮助。感谢。

答案 2 :(得分:0)

创建background_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:bottom="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>

    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:right="1dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</layer-list>

风格

 <style name="AppCompatAlertDialogStyle" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@drawable/background_shadow</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

显示对话框

Dialog dialog = new Dialog(mContext, R.style.AppCompatAlertDialogStyle);
builderSingle.setTitle(getString(R.string.select_quantity));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout);
dialog.show();

希望这能解决您的问题