自定义通用dialog 背景有阴影

2019-03-15  本文已影响0人  一个冬季
效果.png
学习文章

android自定义Dialog弹框和背景阴影显示

style
    <!--通用的dialog-->
    <style name="CommonDialog" parent="android:style/Theme.Dialog">
        <!--背景颜色及和透明程度-->
        <item name="android:windowBackground">@color/transparent</item>
        <!--是否去除标题 -->
        <item name="android:windowNoTitle">true</item>
        <!--是否去除边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--是否模糊-->
        <item name="android:backgroundDimEnabled">false</item>
    </style>
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dra_gray_6600000"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="@dimen/dp_275"
        android:layout_height="@dimen/dp_140"
        android:layout_centerInParent="true"
        android:background="@drawable/rectangular_solid_gray_6600000_corner5"
        android:orientation="vertical">

        <TextView
            android:id="@+id/common_dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/dp_21"
            android:gravity="center"
            android:textColor="@color/dark_222222"
            android:textSize="@dimen/sp_15" />

        <TextView
            android:id="@+id/common_dialog_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_10"
            android:gravity="center"
            android:paddingLeft="@dimen/dp_10"
            android:paddingRight="@dimen/dp_10"
            android:textColor="@color/dark_222222" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"></View>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#E4E4E4" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_40"
            android:orientation="horizontal">

            <Button
                android:id="@+id/common_dialog_cancle"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:text="取消"
                android:textColor="#7D7D7D"
                android:textSize="@dimen/sp_15" />

            <View
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#E4E4E4" />

            <Button
                android:id="@+id/common_dialog_yes"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:text="确定"
                android:textColor="@color/blue_00AAFF"
                android:textSize="@dimen/sp_15" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
颜色
<drawable name="dra_gray_6600000">#66000000</drawable>
圆角
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/white"></solid>
    <corners android:radius="@dimen/dp_5"></corners>
</shape>
Android代码
/**
* @date: 2019/3/15 0015
* @author: gaoxiaoxiong
* @description:初始化通用的dialog
**/
public class CommonDialog extends Dialog implements View.OnClickListener{
    private TextView titleTextView,messageTextView;
    private Button cancleButton,sureButton;
    private OnItemCancleListener onItemCancleListener;
    private OnItemSureListener onItemSureListener;
    private String titleString,messageString;

    public CommonDialog(@NonNull Context context) {
        super(context, R.style.CommonDialog);
    }

    public void setOnItemCancleListener(OnItemCancleListener onItemCancleListener) {
        this.onItemCancleListener = onItemCancleListener;
    }

    public void setOnItemSureListener(OnItemSureListener onItemSureListener) {
        this.onItemSureListener = onItemSureListener;
    }

    public interface OnItemCancleListener{
        void onCancleListener();
    }

    public interface OnItemSureListener{
        void onSureListener();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common);
        setCanceledOnTouchOutside(true);//点击空白的地方,可以取消
        initView();
        initData();
    }

    private void initData(){
        if (titleString!=null)
            titleTextView.setText(titleString);
        if (messageString!=null)
            messageTextView.setText(messageString);
    }

    /**
    * @date: 2019/3/15 0015
    * @author: gaoxiaoxiong
    * @description:设置标题
    **/
    public void setTitleValue(String titleValue){
      this.titleString = titleValue;
    }

    /**
    * @date: 2019/3/15 0015
    * @author: gaoxiaoxiong
    * @description:设置消息内容
    **/
    public void setMessageValue(String messageValue){
        this.messageString = messageValue;
    }



    /**
    * @date: 2019/3/15 0015
    * @author: gaoxiaoxiong
    * @description:初始化view
    **/
    private void initView(){
        titleTextView = this.findViewById(R.id.common_dialog_title);
        messageTextView = this.findViewById(R.id.common_dialog_message);
        cancleButton = this.findViewById(R.id.common_dialog_cancle);
        sureButton = this.findViewById(R.id.common_dialog_yes);
        cancleButton.setOnClickListener(this);
        sureButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.common_dialog_cancle:{//取消
                if (onItemCancleListener!=null){
                    onItemCancleListener.onCancleListener();
                }
            }
            break;

            case R.id.common_dialog_yes:{//确定
                if (onItemSureListener!=null){
                    onItemSureListener.onSureListener();
                }
            }
            break;

        }
    }

    @Override
    public void show() {
        super.show();
        //创造出阴影
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }
}
创造阴影核心代码

要在show后创建

        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
上一篇下一篇

猜你喜欢

热点阅读