how to add shadow effect in alert dialog box in android

后端 未结 3 557
轻奢々
轻奢々 2021-01-03 15:53

i want to add shadow effect in alert dialog box . i want this type of shadow effect in my dialog box here i post 3 files first is style.xml second is theme.java or third fil

相关标签:
3条回答
  • 2021-01-03 16:33

    I know I'm too late. However it might help others who are in need and who want to achieve this in a simple way. Just add this to your parent layout in xml.

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

    for example:

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

    So here LinearLayout is your parent. so adding,

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

    will do it.

    Hope it helps. Thanks.

    0 讨论(0)
  • 2021-01-03 16:48

    I have created custom shadow file (shadow.xml) below

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

    In the next step you should change your dialog theme as what is in the following:

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

    Now you are done, just create a new instance of the Dialog class and apply this theme to it (in Dialog constructor):

    Dialog dialog = new Dialog(this, R.style.dialog_theme);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.mdialog);
    dialog.show();
    
    0 讨论(0)
  • 2021-01-03 16:55

    create 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

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

    Show dialog

    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();
    

    Hope this will solve your problem

    0 讨论(0)
提交回复
热议问题