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,这个 ...
随机推荐
- Spring AOP实现拦截转发控制
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ...
- php 基础入门篇之前言
本人文笔不好,言辞简陋.没有华丽的语句,也没有精致的词语.仅仅是想给大家说一下,和大家一块学习,共同进步.学一点技术,找个吃饭的手段!接下来我会梳理一下知识,算是温故知新,同一时候也算是和大家一起在学 ...
- 深入理解C/C++ [Deep C (and C++)] (2)
好.接着深入理解C/C++之旅.我在翻译第一篇的时候.自己是学到不不少东西,因此打算将这整个ppt翻译完成. 请看以下的代码片段: #include <stdio.h> void foo( ...
- javascript 清空数组的方法
1,splice var ary = [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 [],空数组,即被清空了 2,lengt ...
- RabbitMQ快速入门python教程
摘要:HelloWorld 简介 RabbitMQ:接受消息再传递消息,可以视为一个“邮局”.发送者和接受者通过队列来进行交互,队列的大小可以视为无限的,多个发送者可以发生给一个队列,多个接收者也可以 ...
- Eclips中文版或汉化使用
Eclipse简体中文包下载地址 :http://babel.eclipse.org/babel/ 在上面网站找,下载地址应该是(注意对应的版本): http://www.eclipse.org/do ...
- Knockout JS 演示样例
五个小样例,来自Knockout JS官方站点. //tutorial 1 //following codes uses to demonstrate observable values and ta ...
- int 与 string::length()
今天在代码中遇到这样的问题 ; while (nStart < strTemp.length()) { ... } 感觉自己写的逻辑没有错误,但是,代码执行结果就是不对,结果单步调试到该处发现, ...
- Crypto++库安装、测试
项目中需要使用到C++加密解密库,选择了Crypto++这个开源库,于是先安装并写一个小例子试试 一.下载 网址:http://www.cryptopp.com/#download 二.打开项目 下载 ...
- vue的计算属性
在模板中写入过多的逻辑使模板过重且难以维护.因此有了计算属性(computed)的产生. 你可以像绑定普通属性一样在模板中绑定计算属性,vue知道计算属性中的函数依赖data中的数据.所以当data中 ...