gpt4 book ai didi

android - 为对话框创建阴影

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:30:27 64 4
gpt4 key购买 nike

我想为我的自定义对话框创建一个阴影,这可能吗?

GhazalActivity.public void viewShareMenu() {
Dialog share=new Dialog(this,R.style.shareDialogStyle);
share.setContentView(R.layout.share_popup_layout);
LayoutParams params = share.getWindow().getAttributes();
params.y = this.getResources().getDimensionPixelSize(R.dimen.topbar_height);
params.gravity=(Gravity.RIGHT|Gravity.TOP);
share.getWindow().setAttributes(params);
share.show();
}

样式.xml :

<style name="shareDialogStyle" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:fadeEnabled">true</item>
<item name="android:fadeDuration">1500</item>
<item name="android:shadowColor">@color/temp</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">5</item>
<item name="android:shadowRadius">10</item>
<item name="android:backgroundDimEnabled">false</item>
</style>

share_popup_layout.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dip"
android:background="@color/bg_Ghazal_share_menu"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
style="@style/shareDialogButtons"/>
</LinearLayout>

有什么解决办法吗?

最佳答案

我已经创建了我自己的带有阴影的自定义对话框,您可以根据需要使用它,在第一步中您应该为阴影和对话框框架创建一个 Android 形状。这是我提供的 (dialog_frame_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="my_dialog_theme">
...
<item name="android:windowBackground">@drawable/dialog_frame_shadow</item>
...
</style>

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

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

这是它的截图:
enter image description here

关于android - 为对话框创建阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786369/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com