首页 > 解决方案 > 在 Xamarin.Android Toolbar.axml 中使用下载的图像

问题描述

为了能够在我的 Xamarin 表单应用程序(在 Android 案例中)的导航标题栏的右侧显示徽标,我在本机 Xamarin.Android 项目的 Toolbar.axml 文件中使用它。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<RelativeLayout android:layout_width="wrap_content"
        android:layout_height="fill_parent">

<ImageView
android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/xlogo_small" 
        android:layout_gravity="right"
        android:layout_alignParentRight="true" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>

在 android:src 中,我使用的是 xlogo_small 图像文件,它需要位于 drawables 文件夹中。现在我希望在运行时将该文件动态地(通过 Web 服务调用)添加到 drawables 文件夹中。经过快速搜索后,我发现在运行时无法将文件添加到可绘制文件夹中。

我还发现,在这种情况下,图像应该保存到内部存储器中。在 Xamarin Forms Shared 项目中,我可以通过 Web 服务调用检索徽标并将其保存到:Environment.SpecialFolder.Personal 目录。

我想知道是否有任何方法可以引用存储在此内部存储目录中的文件的路径,并在 Resources.axml 的这一行中使用它而不是 @drawable/xlogo_small:

android:src="@drawable/xlogo_small" 

任何指针?

标签: androidxamarinxamarin.formsxamarin.android

解决方案


推荐阅读