android - 我可以在 Android 中使用不同的带有颜色的 xml 文件吗?

标签 android

我的 Android 应用程序中有一个带有颜色的 xml 文件。 我可以在代码中选择其中之一吗?

类似于:

(if value == 0) {
   // I choose the file 'a'
} else {
  // I choose the file 'b'
}

这可能吗?

提前致谢。

最佳答案

你可以这样做:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v;
if(value == 0){
    v = inflater.inflate(R.layout.file_yellow, null);
}else{
    v = inflater.inflate(R.layout.file_green, null);
}
// do stuff with view

关于android - 我可以在 Android 中使用不同的带有颜色的 xml 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21017483/

相关文章:

java - 致命异常 : Main Android Eclipse

android - GCM 的 API key

android - 找不到 Gradle DSL 方法 :’buildConfigFields‘

Phonegap 应用程序上的 Android Datepicker 崩溃

android - 如何使用水平 ScrollView / ListView 添加动态图像

java - 从 Firebase 数据库读取特定数据

java - 在 ViewRootImpl.java 中创建线程

android - 复选框未显示在 android 布局上

java - 无法更改 MapView 高度

android - MediaStore.Audio.Albums._ID 因歌曲而异