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