Android中Context的理解及使用(一)——Context的作用
Context的作用:用来访问全局信息的接口,通过Context进行资源的访问。
1、Context获取字符串资源:
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
// tv.setText("Hello Android!");
// public final void setText(CharSequence text) { setText(text, mBufferType); }
tv.setText(R.string.hello_android); //strings.xml
//public final void setText(@StringRes int resid) { setText(getContext().getResources().getText(resid)); }
setContentView(tv);
System.out.println(getResources().getText(R.string.hello_android));
}
}
2、Context获取图标资源:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView iv = new ImageView(this);
iv.setImageResource(R.mipmap.ic_launcher);
setContentView(iv);
}
}
Android中Context的理解及使用(一)——Context的作用的更多相关文章
- Android中一个经典理解误区的剖析
今天,在Q群中有网友(@广州-包晴天)发出了网上的一个相对经典的问题,问题具体见下图. 本来是无意写此文的,但群里多个网友热情不好推却,于是,撰此文予以分析. 从这个问题的陈述中,我们发现,提问者明显 ...
- Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...
- 【转】android中layout_weight的理解
android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...
- 9.png(9位图)在android中作为background使用导致居中属性不起作用的解决方法
在使用到9.png的布局上面添加 android:padding="0dip" 比如 <LinearLayout android:layout_widt ...
- Android中Context解析
Context概念 当我们访问当前应用的资源,启动一个新的activity的时候都需要提供Context. Context是一个抽象基类,我们通过它访问当前包的资源(getResources.getA ...
- 网站开发进阶(三十四)编码中的setCharacterEncoding 理解
编码中的setCharacterEncoding 理解 1.pageEncoding="UTF-8"的作用是设置JSP编译成Servlet时使用的编码. 2.contentType ...
- Android中的GraphicBuffer同步机制-Fence
Fence是一种同步机制,在Android里主要用于图形系统中GraphicBuffer的同步.那它和已有同步机制相比有什么特点呢?它主要被用来处理跨硬件的情况.尤其是CPU.GPU和HWC之间的同步 ...
- Android应用开发中关于this.context=context的理解
在Android应用开发中,有的类里面需要声明一个Context的成员变量,然后还需要在该类的构造函数中加上this.context=context;这行代码.为什么要这么写呢?不写不行么? 先看下面 ...
随机推荐
- 烂泥:python2.7和python3.5源码安装
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前几天在centos6.6安装ansible时,一直提示python版本不对,导致不能安 ...
- QStatusBar的用法
QStatusBa,状态栏是位于主窗口的最下方,提供一个显示工具提示等信息的地方.QMainWindow类里面就有一个statusBar()函数,用于实现状态栏的调用.以下例子都在QMainWin ...
- [python]获取网页中内容为汉字的字符串的判断
实际上是这样,将获取到网页中表单内容与汉字字符串作比较,即: a = request.POST['a'] if a == '博客园': print 'ok' else: print 'false' a ...
- css3 text-overflow和white-space,word-wrap,word-break进阶
一.text-overflow 1.text-overflow作用 简单来说,text-overflow用来设置文本溢出时是否用省略标记(...) 准确的说是设置溢出的文本在不显示的情况下怎样展示给用 ...
- 高级查询---嵌套and分页
高级嵌套语句: 子查询: 语句: select * from 表名 where 列名= ( 子查询语句 ) 注意:子查询语句必须放在小括号呢 可以使用< >=等运算符号,sql serve ...
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...
- [LeetCode] UTF-8 Validation 编码验证
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte char ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- mysql-5.7.11-winx64.zip 安装配置
1.下载 http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.11-winx64.zip 2.解压缩zip包: D:\Program Files\m ...
- 5sing分析
0x1.抓包数据:用fiddler抓取不到,用smartsniff [6/26/星期日 18:05:04:391]GET /user/login?username=15081515272&pa ...