android 怎么获取app 字体颜色,android app 修改字体

android中可能会遇到修改字体的情况,虽然说需求比较少,但是偶尔还会遇到

可以使用三方框架来帮助我们简单做到

api "uk.co.chrisjenx:calligraphy:2.2.0"

在style文件中添加一个字体的style

fonts/Roboto-Thin.ttf

我app中的main/assert/fonts/Roboto-hin.ttf    放置字体文件

AS集成这个框架,在activity的  attachBaseContext(Context newBase)方法中添加我们修改后的字体的context

protected void attachBaseContext(Context newBase) {

super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));

}

为什么要在attachBaseContext中添加呢?  我们都知道activity中是有context的,但是context是怎么来的呢?

activity的构造方法是无参构造,所以不是通过构造方法传递的吗?

activity的集成流程  context(抽象类) -> ContextWrapper(实现了context的所有方法,service的父类) -> ContextThemeWrapper(带主体的context) -> activity

再看下activity的启动流程  :    activity启动是先执行了  attach方法   下面才回去执行onCreate   如果我们想把一些初始化放到最高的优先级,可以放在 attachBaseContext中。

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {

Activity activity = null;

try {

java.lang.ClassLoader cl = r.packageInfo.getClassLoader();

activity = mInstrumentation.newActivity(

cl, component.getClassName(), r.intent);

} catch (Exception e) {

}

try {

if (activity != null) {

ContextImpl appContext = new ContextImpl();

appContext.init(r.packageInfo, r.token, this);

appContext.setOuterContext(activity);

CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());

Configuration config = new Configuration(mCompatConfiguration);

activity.attach(appContext, this, getInstrumentation(), r.token,

r.ident, app, r.intent, r.activityInfo, title, r.parent,

r.embeddedID, r.lastNonConfigurationInstances, config);

attachBaseContext到底干了什么呢?  代码其实很简单,就是把context赋值给activity,所以我们在把newbase添加了字体,所以activity的context,都会有我们的字体效果。

需要注意一点,就是每一个界面都有一个context,所以建议写一个baseActivity,直接继承。

protected void attachBaseContext(Context base) {

if (mBase != null) {

throw new IllegalStateException("Base context already set");

}

mBase = base;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441601.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1254 】推箱子 (双bfs)

题干: 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙…

android 退出函数,android – 关闭应用程序与退出按钮

下面使用main.xml文件android:orientation"vertical" android:layout_width"fill_parent"android:layout_height"fill_parent">android:layout_height"wrap_content" android:id"id/txt1" android:text"txt1" …

算法讲解 -- 莫队算法

摘要: 莫队算法是一个对于区间、树或其他结构离线(在线)维护的算法,此算法基于一些基本算法,例如暴力维护,树状数组,分块,最小曼哈顿距离生成树,对其进行揉合从而产生的一…

android标题栏消失,安卓标题栏为什么没有显示

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼用Eclipse开发的APP,其中一个Activit的标题栏没有显示,但没对Activity做过任何配置。由于不知道哪里出问题,下面把Activity的java文件、布局文件和Mainfest全部放出来Activity.java-------------…

知识点 组合数学 卡特兰数

关于卡特兰数 卡特兰数是一种经典的组合数,经常出现在各种计算中,其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 914…

android滑动菜单图标,Android实现简单底部导航栏 Android仿微信滑动切换效果

Android仿微信滑动切换最终实现效果:大体思路:1. 主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标;2. 底部导航栏的设置方法类似于TabLayout的关联,View需要创建关联方法,用来关联VIewPag…

【HDU - 2149】Public Sale (巴什博奕)

题干: 虽然不想,但是现实总归是现实,Lele始终没有逃过退学的命运,因为他没有拿到奖学金。现在等待他的,就是像FarmJohn一样的农田生涯。 要种田得有田才行,Lele听说街上正在举行一场别开生面的拍卖会&…

Android万能遥控菜单选择添加,Android万能遥控器小应用

在很久很久以前,手机是有红外功能的,后来随着蓝牙技术的成熟,红外逐渐被蓝牙取代,不再是标配了。红外本身还是有些优点,比如操作简便,成本低。要想在手机上添加红外功能,就要外接一个转换模块。…

android studio 库工程,Android Studio 添加已有工程方法

准备工作:修改 excluded-paths和android.iml,修改内容详见下图。(目的:过滤和优先在sourcefolder查找,若没有再到JAR包中查找)BorqsUI/LINUX/android/development/tools/idegen$ mm编译出来:[100% 3/3] Install: out/h…

【HDU - 3951】Coin Game (博弈,猜规律,对称博弈)

题干: After hh has learned how to play Nim game, he begins to try another coin game which seems much easier. The game goes like this: Two players start the game with a circle of n coins. They take coins from the circle in turn and every time…

android textview 白色,android – AutoCompleteTextview默认情况下,颜色设置为白色

我在我的Android应用程序中使用了一个AutoCompleteTextView,它正常工作。我唯一遇到的问题是,默认情况下,建议的颜色为白色,我无法看到任何建议。所以当我开始打字时,列表会以白色条目(不可见)扩展,但是当我…

【HDU - 1527】【POJ - 1067】取石子游戏 (威佐夫博弈)

题干: 有两堆石子,数量任意,可以不同。游戏开始由两个人轮流取石子。游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子;二是可以在两堆中同时取走相同数量的石子。最后把石子全部取…

android okgo参数,Android OkGo基本操作

或许不是很全面,也都是从网上粘的,自己记下以后使用方便点。如有不对的地方 欢迎指教。首先添加依赖:implementation com.lzy.net:okgo:3.0.4设置初始化和全局配置:public class App extends Application {Overridepublic void on…

【CodeForces - 334B】Eight Point Sets(水题模拟,有坑)

题干: Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, ex…

一加6怎么刷android p6,一加6秒速跟进安卓P 教你尝鲜速成开发者

今年5月份,谷歌在I/O开发者大会上发布了全新的Android P操作系统,而在Android P系统发布后的没多久,一加手机官方就公开承诺,年度旗舰一加手机6将会成为首批次升级谷歌Android P系统的机型。而现在,一加已经开始兑现此…

*【CodeForces - 1047A】Little C Loves 3 I (水题,构造,三元组问题)

题干: Little C loves number 3 very much. He loves all things about it. Now he has a positive integer nn. He wants to split nn into 33 positive integers a,b,ca,b,c, such that abcnabcn and none of the 33 integers is a multiple of 33. Help him to…

html语言音乐添加路径,HTML5简单实现添加背景音乐的几种方法

这里推荐两种方法,就是两个标签 或者常用 css布局 隐藏播放器 做网站比较实用!html5添加音乐说明:1、src毫无疑问写路径.2、使用hidden"true"表示隐藏音乐播放按钮,相反使用hidden"false"表示开启音乐播放按钮…

【CodeForces - 1047B 】Cover Points (数学,构造,思维)

题干: There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle …

html字段隐藏,如何刮取动态隐藏的HTML字段(UuViewState)值?

我在代码中完全实现了请求,但在FormRequest中意识到VIEWSTATE和EVENTVALIDATION是动态的,它们随每个请求而变化。我想做的是刮去它们以便在下一个请求中提供它们。在这两个字段都在HTML代码中提供,但隐藏了<the __VIEWSTAT…

【CodeForces - 1051A】Vasya And Password (构造,水题)

题干: Vasya came up with a password to register for EatForces — a string ss. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its u…