如何为弹出窗口提供弯曲的边缘并为弹出窗口提供明显的阴影

时间:2019-06-27 08:25:12

标签: java android android-layout android-xml

我必须为弹出窗口提供弯曲的边缘,使弹出窗口具有明显的阴影,但工作正常,但在没有弯曲边缘和明显阴影的情况下,背景窗口和弹出窗口在b /中未显示明显的空间他们

    DisplayMetrics dm=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width=dm.widthPixels;
    int height=dm.heightPixels;
    getWindow().setLayout((int)(width*1.0),(int)(height*.8));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        getWindow().setElevation(40);
    }

1 个答案:

答案 0 :(得分:2)

答案很简单,请尝试以下操作:-

首先创建一个自定义弹出窗口,并为其分配自定义布局。

private fun channelPopup(){

        //   addDummyProfile()
        channelDialog = Dialog(context)
        channelDialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val lp = channelDialog!!.getWindow()!!.getAttributes()
        val window = channelDialog!!.getWindow()
        window!!.setGravity(Gravity.CENTER)
        channelDialog!!.getWindow()!!.setAttributes(lp)
        channelDialog!!.getWindow()!!.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        channelDialog!!.setCanceledOnTouchOutside(true)
        channelDialog!!.setCancelable(true)
        channelDialog!!.setContentView(R.layout.exitsing_channel_layout)
        val tv_videos_no: TextView = channelDialog!!.findViewById(R.id.tv_videos_no) as TextView
        val recycler_channel = channelDialog!!.findViewById<RecyclerView>(R.id.recycler_channel)
        recycler_channel!!.layoutManager= LinearLayoutManager(context)
        channelAdapter = ChannelAdapter(tv_videos_no)
        recycler_channel!!.adapter = channelAdapter
        channelDialog!!.show()
    }

,然后在您的布局中为父布局提供自定义背景,创建一个新的可绘制xml并将其粘贴:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/white"/>
    <corners
        android:radius="30dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

,然后将此可绘制XML设置为自定义弹出式布局XML的父级布局的背景。

将此行添加到自定义弹出Java文件中。

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));