Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.3——整合resource文件
问题:
想要在product的flavor里面改变图片,文字或者其它资源。
解决方案:
在flavor里面增加合适的资源目录,并且改变他们包含的值。
讨论:
考虑下3.2章的“hello world with attitude”应用,它定义了三个flavors:arrogant,friendly和obsequious。在每个情况下,app提示用户输入姓名,并且用这个姓名欢迎用户。每个的java代码都是相同的,但是看上去和感觉上好像每个都不一样。
product的flavors在gradle配置文件中像下面这样定义:

每个flavor都有一个不同的applicationId,这样他们可以被安装到同一台设备上供演示使用。
下面的示例,显示一个MainActivity类的onCreate和sayHello方法:
public class MainActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.name_edit_text);
}
public void sayHello(View view) {
String name = editText.getText().toString();
Intent intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("user", name);
startActivity(intent);
}
}
这个activity有一个EditText属性,用来收集用户姓名。sayHello方法收集这个名字,并且将它作为extra放入intent中,并用这个intent启动welcomeActivity。
mainActivity的layout只是一个简单的包含textview,editText,button的linearLayout。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/name_edit_text"
android:hint="@string/name_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:onClick="sayHello"
android:text="@string/hello_button_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity是启动项。下面展示arrogant flavor自定义的初始屏幕:

应用是如何命名和初始化设置?三个flavor都有自己的resources目录,在app/<flavor>/res下面。每个情况下都添加一个叫做values的子目录,并且将string.xml从app/src/main/res/values目录下拷贝进去。arrogant flavor的string.xml如下:
<resources>
<string name="app_name">Arrogant</string>
<string name="title_activity_welcome">His/Her Royal Highness</string>
<string name="hello_world">Arrogant</string>
<string name="greeting">We condescend to acknoweldge your presence, if just barely, %1$s.</string>
</resources>
arrogant的项目结构如下图:

通过整合build type和主目录树下相同的文件夹中的res文件夹下的values实现整合资源。优先的是build type覆盖 product flavor,覆盖main source。
ps:非java资源互相覆盖,build type拥有最高优先级,其次是flavor,在后面是main目录。
welcomeActivity有一个onCreate方法接受用户姓名,并且和用户打招呼。
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
String name = getIntent().getStringExtra("user");
TextView greetingText = (TextView) findViewById(R.id.greeting_text);
String format = getString(R.string.greeting);
greetingText.setText(String.format(format, name));
}
}
welcomeActivity的layout包含了一个textView和image。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.oreilly.helloworld.WelcomeActivity">
<TextView
android:id="@+id/greeting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="24sp"
android:drawableBottom="@drawable/animal" />
</LinearLayout>
每个flavor都有自己的values.xml和animal.png。
每个flavor都是相同的处理。obsequious flavor使用的strings.xml如下:
<resources>
<string name="app_name">Obsequious</string>
<string name="hello_world">Obsequious</string>
<string name="title_activity_welcome">your humble servant</string>
<string name="greeting">O great %1$s, please accept this pathetic
greeting from my unworthy self. I grovel in your
general direction.</string>
</resources>
friendly flavor的strings.xml如下:
<resources>
<string name="app_name">Friendly</string>
<string name="title_activity_welcome">We are BFFs!</string>
<string name="hello_world">Friendly</string>
<string name="greeting">Hi there, %1$s!</string>
</resources>
arrogant的欢迎界面:

friendly的欢迎界面:

obsequious的欢迎界面:

整合非java代码是容易的。只要增加合适的文件夹和文件,就可以覆盖main下面的文件。
Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.3——整合resource文件的更多相关文章
- Android开发工具全面转向Android Studio(3)——AS project/module的目录结构(与Eclipse对比)
如果AS完全还没摸懂的,建议先看下Android开发工具全面转向Android Studio(2)——AS project/module的CRUD. 注:以下以Windows平台为标准,AS以目前最新 ...
- Android开发工具全面转向Android Studio(2)——AS project/module的CRUD
本文有些地方可能需要衔接Android开发工具全面转向Android Studio(1)——准备开发环境,读起来效果会更好. 这个世界很奇妙,所有的东西离不开CRUD,即增删改查.即使人本身也遵循这个 ...
- 配置cordova的android开发环境(无android studio)
原文:配置cordova的android开发环境(无android studio) 趁元旦放假想试一下cordova,不想安装庞大的android studio,所以想最小化安装,居然花了一整天的时间 ...
- 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。
AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...
- Eclipse搭建Android开发环境并运行Android项目
Eclipse搭建Android开发环境并运行Android项目 (详细) 安装环境: window 10 64位 安装工具: JDK.Eclipse.SDK.ADT 安装步骤: 1.JAVA JDK ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)6.2——DSL文档
问题: 你需要查找Android Gradle DSL的完整文档. 解决方案: 访问Gradle Tools网站,从Android开发网站下载ZIP文件. 讨论:Android开发网站首页有完整的AP ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.1——单元测试
问题: 你想要测试app中的非android部分. 解决方案: 可以使用Android Studio1.1里面增加的单元测支持和Android的Gradle插件. 讨论: ADT插件只支持集成测试,并 ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.3——用Eclipse ADT导出App
问题: 想在一个已经存在的Eclipse ADT的项目中使用Gradle 解决方案: Eclipse ADT插件可以帮助生成Gradle文件 讨论: Eclipse的ADT插件是在2013年推出Gra ...
- Android开发:《Gradle Recipes for Android》阅读笔记1.2
在android开发中会需要配置使用app的android SDK的最低版本和目标版本,这个是bulidl.gradle的android模块设置.默认有以下几个设置: applicationId,这个 ...
随机推荐
- 实现Xshell断开连接情况下Linux命令继续执行
1.将原命令语句改为:nohup 命令语句 & 2.回车执行,再回车,窗口中会显示一个进程号 3.如果中途想关闭,可执行:kill -9 进程号.如果想查看命令执行情况,可执行:cat noh ...
- [转] Google 开源 iOS 应用测试工具:EarlGrey
Google 开源 iOS 应用测试工具:EarlGrey oschina 发布于: 2016年02月18日 (3评) 分享到: 收藏 +53 3月19日,深圳源创会火热报名中,go>&g ...
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- 倍福TwinCAT(贝福Beckhoff)应用教程12.3 TwinCAT控制松下伺服 NC进阶
在前面一节,我们简单介绍了通过PLC+HMI实现完整控制松下伺服的上使能-运动,采集位置,速度等功能,这里我们会大量简化用到的贝福功能块(为了更加实用).首先依然是对单个轴的封装,我们之前的做法,例如 ...
- <四>读<<大话设计模式>>之代理模式
代理模式我想大家即便不熟悉也都听过吧,从字面意思上看就是替别人干活的,比方代理商.在项目的实际应用中也有非常多地方用到.比方spring通过代理模式生成对象等. 代理模式的书面定义:为其它对象提供一种 ...
- spring自己主动装配Bean属性
spring提供了3种类型的自己主动装配 byName:把与Bean的属性具有同样名字(或者ID)的其它Bean自己主动装配到Bean的相应属性中. byType:把与Bean的属性具有同样类型的其它 ...
- python(30)- 常用模块
模块就是py文件.python中能开辟作用域的只有函数.类和模块. for循环不能开辟作用域,for循环内的变量为全局变量.if...else...同for循环一样. 一 time模块 时间表示形式 ...
- mongoDB group命令详解
http://heipark.iteye.com/blog/1167948 http://rjhym.iteye.com/blog/1224200 http://blog.163.com/ ...
- Spring Cloud简单入门教程
原文地址:http://www.cnblogs.com/skyblog/p/5127690.html 按照官方的话说:Spring Cloud 为开发者提供了在分布式系统(如配置管理.服务发现.断路器 ...
- 基于tornado实现web camera
基于tornado实现web camera 近期在学习python.找了一个框架学习,我选择的是tornado.由于其不仅仅是一个web开发框架,其还是一个server,异步事件库,一举多得. 我一直 ...