在 Android Studio 中静态添加一个 ImageView
Add an ImageView statically in Android Studio
我想从 android studio 中的变量更改图像视图的资源。
让我解释一下,我有第一个 activity “选择器 activity”,它有 21 张图片,在每次点击图片时,一个变量 god_name 被更改为特定的神,现在我发送那个使用 putExtra 的变量,现在我希望根据变量更改 MainActivity 中 ImageView 的资源,例如,如果变量是“二”,那么我想将资源更改为“R.drawable.two”。
诸如此类,我在 python 方面有一些经验,所以我在那里使用了 f 字符串,我可以在这里做点什么吗?
MainActivity.class
(注意-我没有包含导入,所以允许上传代码。)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String god_name = getIntent().getExtras().getString("chosen_god").toString();
ImageView god = findViewById(R.id.god);
god.setImageResource();
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity">
<ImageView
android:id="@+id/god"
android:layout_width="413dp"
android:layout_height="496dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/hanuman" />
</androidx.constraintlayout.widget.ConstraintLayout>
我可以使用多个 if 条件和 switch case 语句,但是那会很大,我不得不写 21 次,所以非常困难!
提前感谢任何人的帮助!
听起来您需要使用 resources.getIdentifier()
才能完成这项工作。
我曾经写过一个相关主题的答案:
无论如何,您需要的是传递资源名称,然后解析标识符以使用它。所以像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// make sure that god_name is the name of the resource, so AFTER R.mipmap.[resourceName]
// so for R.mipmap.two you need to pass "two" as "chosen_god" extra
String god_name = getIntent().getExtras().getString("chosen_god").toString();
ImageView god = findViewById(R.id.god);
int resId = getResources().getIdentifier(god_name, "mipmap", getPackageName());
god.setImageResource(resId);
}
我想从 android studio 中的变量更改图像视图的资源。 让我解释一下,我有第一个 activity “选择器 activity”,它有 21 张图片,在每次点击图片时,一个变量 god_name 被更改为特定的神,现在我发送那个使用 putExtra 的变量,现在我希望根据变量更改 MainActivity 中 ImageView 的资源,例如,如果变量是“二”,那么我想将资源更改为“R.drawable.two”。 诸如此类,我在 python 方面有一些经验,所以我在那里使用了 f 字符串,我可以在这里做点什么吗?
MainActivity.class (注意-我没有包含导入,所以允许上传代码。)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String god_name = getIntent().getExtras().getString("chosen_god").toString();
ImageView god = findViewById(R.id.god);
god.setImageResource();
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity">
<ImageView
android:id="@+id/god"
android:layout_width="413dp"
android:layout_height="496dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/hanuman" />
</androidx.constraintlayout.widget.ConstraintLayout>
我可以使用多个 if 条件和 switch case 语句,但是那会很大,我不得不写 21 次,所以非常困难! 提前感谢任何人的帮助!
听起来您需要使用 resources.getIdentifier()
才能完成这项工作。
我曾经写过一个相关主题的答案:
无论如何,您需要的是传递资源名称,然后解析标识符以使用它。所以像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// make sure that god_name is the name of the resource, so AFTER R.mipmap.[resourceName]
// so for R.mipmap.two you need to pass "two" as "chosen_god" extra
String god_name = getIntent().getExtras().getString("chosen_god").toString();
ImageView god = findViewById(R.id.god);
int resId = getResources().getIdentifier(god_name, "mipmap", getPackageName());
god.setImageResource(resId);
}