您的位置:首页 > 编程语言 > Java开发

通过R.java文件访问资源

2016-09-16 20:55 316 查看
本文信息来自:http://www.runoob.com/w3cnote/android-tutorial-project-src-analysis.html

一.在java代码中

   Java 文字:

txtName.setText(getResources().getText(R.string.name));
   图片:

imgIcon.setBackgroundDrawableResource(R.drawable.icon);
颜色:
txtName.setTextColor(getResouces().getColor(R.color.red));
布局:
setContentView(R.layout.main);
控件:
txtName = (TextView)findViewById(R.id.txt_name);控件:
txtName = (TextView)findViewById(R.id.txt_name);

XML代码中使用:

通过@xxx即可得到,比如这里获取文本和图片:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background = "@drawable/img_back"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: