2017-01-18 189 views
1

我想在提醒對話框中添加陰影效果。 i want this type of shadow effect in my dialog box 這裏我張貼的3個文件首先是style.xml二是theme.java或第三個文件是demo_bg.xml file.i已經嘗試這樣如何在提醒對話框中添加陰影效果android

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>

+0

你的demo_bg是什麼? –

+0

最簡單的方法是創建自己的佈局,並使佈局的CardView'根元素 –

+0

它是一個影子文件@RahulDevanavar –

回答

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

希望這將解決您的問題

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 = new Dialog(this, R.style.dialog_theme); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.mdialog); 
dialog.show(); 
+0

謝謝你的幫助@Pranav Darji –

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" 

會做到這一點。

希望它有幫助。謝謝。