Android 显示来自其名称(Kotlin)的图像
我正在寻找一种方法来显示一个图像从绘图文件夹的名称。 解释:我有一个Back4App解析数据库。图像由其名称在字符串类型的列中引用。 例如:在我的数据库中,我将有如下引用的成员 姓 名字 我的应用程序的可绘图文件夹中的喜爱运动(这些是他所从事运动的象形图)图像 照片(name=JamesPhoto)照片位于我的应用程序的可绘制文件夹中 如何通过指示图像的名称在图像视图中显示图像? 目前,我显示的照片,其Id。见下面的代码Android 显示来自其名称(Kotlin)的图像,android,image,kotlin,parse-platform,Android,Image,Kotlin,Parse Platform,我正在寻找一种方法来显示一个图像从绘图文件夹的名称。 解释:我有一个Back4App解析数据库。图像由其名称在字符串类型的列中引用。 例如:在我的数据库中,我将有如下引用的成员 姓 名字 我的应用程序的可绘图文件夹中的喜爱运动(这些是他所从事运动的象形图)图像 照片(name=JamesPhoto)照片位于我的应用程序的可绘制文件夹中 如何通过指示图像的名称在图像视图中显示图像? 目前,我显示的照片,其Id。见下面的代码 <?xml version="1.0"
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="16dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/pictos"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/picto1"
android:layout_alignParentRight="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pt_temple"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/picto2"
android:layout_alignParentRight="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pt_surf"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/picto3"
android:layout_alignParentRight="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pt_tradition"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/picto4"
android:layout_alignParentRight="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pt_art"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/picto5"
android:layout_alignParentRight="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pt_kite"/>
</LinearLayout>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pictos"
android:layout_alignParentLeft="true"
android:layout_marginLeft="-2dp"
android:layout_marginTop="6dp"
android:layout_marginRight="2dp"
android:layout_toLeftOf="@+id/imageLieu"
android:text="Nom lieu"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/memberPhoto"
android:layout_alignParentRight="true"
android:layout_width="80pt"
android:layout_height="43pt"
android:src="@drawable/james"/>
</RelativeLayout>
<TextView
android:id="@+id/Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:lines="4"
android:text="@string/text_generic"
android:textColor="#050000" />
<LinearLayout
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/favorisNoir"
android:layout_width="35dp"
android:layout_height="35dp"
android:width="35dp"
android:height="35dp"
android:background="@drawable/favoris_noir" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/like"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/like" />
<Space
android:layout_width="5dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/countLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/like"
android:focusable="false"
android:gravity="center_horizontal"
android:text="100"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
您可以使用
getIdentifier
获取资源。使用下面的代码段按名称从资源中获取可绘制的内容
pic1
是您的可绘制图像名称drawable
是资源文件夹android.demo
是您的包名(或者您可以使用activity.getPackageName()
动态获取包)val identifier = resources.getIdentifier("pic1", "drawable", "android.demo")
imageView1.setImageResource(identifier)
爪哇
或者您可以使用反射来获取资源
try {
Class res = R.drawable.class;
Field field = res.getField("pic1");
int drawableId = field.getInt(null);
image.setImageResource(identifier);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
您可以使用
getIdentifier
获取资源。使用下面的代码段按名称从资源中获取可绘制的内容
pic1
是您的可绘制图像名称drawable
是资源文件夹android.demo
是您的包名(或者您可以使用activity.getPackageName()
动态获取包)val identifier = resources.getIdentifier("pic1", "drawable", "android.demo")
imageView1.setImageResource(identifier)
爪哇
或者您可以使用反射来获取资源
try {
Class res = R.drawable.class;
Field field = res.getField("pic1");
int drawableId = field.getInt(null);
image.setImageResource(identifier);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
感谢您的反馈,对于kotlin来说,我将尝试应用它。一个问题,是否不可能创建一个文件或其他文件,允许我在整个应用程序中使用这种可能性,因为它将是常规的。感谢您感谢您的反馈,对于kotlin来说,我是新手,我会尝试应用它。一个问题,是否不可能创建一个文件或其他文件,允许我在整个应用程序中使用这种可能性,因为它将是常规的。非常感谢。