这两天有一个需求:设置dialog圆角,写完后发现并没有达到效果,以前也碰到这个问题,这里记录下解决方案,便于查阅。也有百度去查询原因,却没有发现合适的解答,当然更可能是我没找到,还是自己解决吧。

Dialog与DialogFragment 解决方案一致:只要设置背景透明解决问题了。

 

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        setContentView(view);        setCancelable(true);    }
    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        //设置背景透明        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        View view = View.inflate(getActivity(), R.layout.dialog_zntchain, null);        return view;    }

看到这里,不难明白,应该是google自己设置了默认的背景,虽然我们设置了view为圆角,但是在挂在已有背景下,我们就是自然看不到,看下源码,google也有说道透明问题。

 

 

 /**     * Change the background of this window to a Drawable resource. Setting the     * background to null will make the window be opaque. To make the window     * transparent, you can use an empty drawable (for instance a ColorDrawable     * with the color 0 or the system drawable android:drawable/empty.)     *     * @param resId The resource identifier of a drawable resource which will     *              be installed as the new background.     */    public void setBackgroundDrawableResource(@DrawableRes int resId) {        setBackgroundDrawable(mContext.getDrawable(resId));    }

 

 

 

 

 


 

 

更多相关文章

  1. Notification探险
  2. android 通过jdbc访问mysql数据库--(含android studio 配置)通过测
  3. Android(安卓)Design Library之四:BottomSheetDialog
  4. Android布局之二——Relative Layout
  5. 【攻克Android(安卓)(43)】WebView (网络视图)
  6. 为Activity屏幕的标题添加图标
  7. Android(安卓)样式 :shape selector 篇
  8. Android(安卓)CoordinatorLayout+AppBarLayout实现toolbar背景渐
  9. 怎样将Activity设置成窗口模式

随机推荐

  1. android 重用 c代码
  2. android studio 中一些比较好的插件
  3. 系出名门Android(4) - 活动(Activity),
  4. Android(安卓)HDMI( 三)
  5. Android中TextVIew一些属性
  6. Android(安卓)支持多屏幕机制
  7. Android(安卓)中文 API (102)—— CursorAd
  8. Android网络应用接口
  9. 系出名门Android(5) - 控件(View)
  10. Android(安卓)Studio加速