【发布时间】:2021-03-19 17:31:27
【问题描述】:
我刚开始使用 Glide 将图像加载到我的 Android 应用程序中。我在片段中使用它,我想将工具栏的背景设置为 URL 中的某个图形。不幸的是,滑翔不显示图片,而只是工具栏中的白色背景。工具栏本身不是问题,因为我可以用颜色改变它的背景,这很有效。还可以显示 App 文件夹中保存的图像(我将其存储在 drawable 文件夹中)。
这是我在片段的 onCreateView 方法中执行的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentGenericBinding.inflate(inflater, container, false);
ImageView image = new ImageView(getContext());
image.setDrawingCacheEnabled(true);
Glide.with(this).load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
.into(image);
Bitmap bmap = image.getDrawingCache();
binding.toolbarGenericMenu.setBackground(new BitmapDrawable(getResources(), bmap));
//This code works at creates a green background
//ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(getContext(), R.color.colorGreen));
//binding.toolbarGenericDrinkMenu.setBackground(colorDrawable);
//This code also works and display an image from R.drawable on the toolbar
// binding.toolbarGenericMenu.setBackgroundResource(R.drawable.test_foto);
return binding.getRoot();
}
知道为什么使用 Glide 加载图像不起作用吗?我会很感激每一条评论。
【问题讨论】:
-
提供日志以及为什么 new ImageView(getContext());?
-
你没有提到你使用的是什么类型的工具栏,所以检查这个post,具体如何使用.into()。
-
感谢 Eyosiyas 的评论。好吧,我必须以某种方式实例化 ImageView。我也试过“ImageView image = null;”但它也确实有效。所以我使用了“new ImageView(getContext());”。你对我有什么建议
-
感谢 javdromero 的评论。我使用 androidx.appcompat.widget.Toolbar
标签: java android android-glide