Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使颤振androidview背景透明或设置androidview背景?_Android_Flutter - Fatal编程技术网

如何使颤振androidview背景透明或设置androidview背景?

如何使颤振androidview背景透明或设置androidview背景?,android,flutter,Android,Flutter,我想显示一个来自本机的小部件,并使Android view背景透明,但背景始终为白色,我想知道如何使androidviewbackground透明 颤振代码: Container( width: 300.0, height: 300.0, alignment: Alignment.center, color: Colors.green, child: SizedBox(

我想显示一个来自本机的小部件,并使Android view背景透明,但背景始终为白色,我想知道如何使
androidview
background
透明

颤振代码:

Container(
            width: 300.0,
            height: 300.0,
            alignment: Alignment.center,
            color: Colors.green,
            child: SizedBox(
              width: 250.0,
              height: 250.0,
              child: AndroidView(
                viewType: 'AndroidViewDemo',
                onPlatformViewCreated: (id) {
                  print("onPlatformViewCreated:$id");
                },
              ),
            ),
          )
Android代码

public PlatformView create(Context context, int i, Object o) {
    final ImageView imageView = new ImageView(context);
    imageView.setLayoutParams(new ViewGroup.LayoutParams(300, 300));
    imageView.setImageDrawable(context.getDrawable(R.mipmap.ic_launcher));
    imageView.setBackgroundColor(Color.YELLOW);
    PlatformView view = new PlatformView() {
        @Override
        public View getView() {
            return imageView;
        }
        @Override
        public void dispose() {
        }
    };
    return view;
}  

返回视图之前
在您的Android代码中,只需设置
view.setBackgroundColor(Color.TRANSPARENT)

我知道这个问题很老了,但我也遇到了同样的问题,我花了一些时间在对现有Fluter问题的评论中找到了解决方案:

以下是适合您的代码的Java版本:



    public PlatformView create(Context context, int i, Object o) {
        final ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(300, 300));
        imageView.setImageDrawable(context.getDrawable(R.mipmap.ic_launcher));
        imageView.setBackgroundColor(Color.YELLOW);
        PlatformView view = new PlatformView() {
            @Override
            public View getView() {
                makeWindowTransparent();
                return imageView;
            }

            @Override
            public void dispose() {
            }

            private void makeWindowTransparent() {
                imageView.post(() -> {
                    try {
                        ViewParent parent = imageView.getParent();

                        if(parent == null) return;

                        while(parent.getParent() != null) {
                            parent = parent.getParent();
                        }

                        Object decorView = parent.getClass().getDeclaredMethod("getView").invoke(parent);
                        final Field windowField = decorView.getClass().getDeclaredField("mWindow");
                        windowField.setAccessible(true);

                        final Window window = (Window)windowField.get(decorView);

                        windowField.setAccessible(false);

                        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    } catch(Exception e) {
                        // log the exception
                    }
                });
            }
        };

        return view;
    }