Android学习——android资源访问
2015-09-04 22:12
495 查看
1. 界面跳转:
方式一:Intent
activity_main.xml:<Button android:id="@+id/btn_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second" android:onClick="goSecond" /> |
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goBack" android:text="返回" /> </LinearLayout> |
publicclass MainActivity extends Activity { @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } publicvoid goSecond(View v){ Intent intent = new Intent(this,SecondActivity.class); startActivity(intent); } } |
publicclass SecondActivity extends Activity { @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //加载第二个Activity的布局文件 setContentView(R.layout.activity_second); } publicvoid goBack(View v) { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } } |
<activity android:name="com.example.appdemo1.SecondActivity" android:label="Second" > </activity> |
第一个界面:
点击second按钮即跳转到SecondActivity:
方式二:setContentView()
只有一个MainActivity类,点击按钮的时候切换不同的布局文件,按钮响应事件也都放在这个类中publicvoid goSecond(View v){ setContentView(R.layout.activity_second); } publicvoid goBack(View v) { setContentView(R.layout.activity_main); } |
方式三:View的隐藏和显示View.VISIBLE;
将控件都放在同一个布局文件activity_main.xml,默认不显示第二个界面的TextView和Button控件,将其可见性设为gone:<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" > <Button android:id="@+id/btn_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second" android:onClick="goSecond" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:visibility="gone" /> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goBack" android:text="返回" android:visibility="gone"/> </LinearLayout> |
publicvoid goSecond(View v){ TextView tv = (TextView) this.findViewById(R.id.tv); tv.setVisibility(View.VISIBLE); Button btnBack = (Button) this.findViewById(R.id.btn_back); btnBack.setVisibility(View.VISIBLE); Button btnSecond = (Button) this.findViewById(R.id.btn_second); btnSecond.setVisibility(View.GONE); } publicvoid goBack(View v){ TextView tv = (TextView) this.findViewById(R.id.tv); tv.setVisibility(View.GONE); Button btnBack = (Button) this.findViewById(R.id.btn_back); btnBack.setVisibility(View.GONE); Button btnSecond = (Button) this.findViewById(R.id.btn_second); btnSecond.setVisibility(View.VISIBLE); } |
VISIBLE可见,
INVISIBLE不可见,位置还在 ,
GONE不可见,位置也不在了
说明:界面的跳转方法一比较慢,二、三较流畅
2.android中共享全局数据(自定义Application)
在清单文件AndroidMainfest.xml文件中配置Application:<application
android:name="com.example.appdemo1.MyApplication">
<activity>
</activity>
</application>
自定义MyApplication,包含属性score:
publicclass MyApplication extends Application{ privatestaticintscore;//设为静态 /** * @return the score */ publicint getScore() { returnscore; } /** * @param score the score to set */ publicvoid setScore(intscore) { this.score = score; } } |
MyApplication app = (MyApplication) this.getApplication(); app.setScore(100); |
MyApplication app = (MyApplication) this.getApplication(); Toast.makeText(this, "score="+app.getScore(), Toast.LENGTH_LONG).show(); |
3. android中资源访问
(1)res与assets同:都可以存放资源文件
异:
res目录下的资源会在R文件中建立索引;
assets目录下的不会在R文件中建立索引,需要通过流进行读取,一般保存原生文件,如mp3
(2)资源的两种访问方式:
i. 在java代码中使用Context.getResources()方法得到Resources对象,该对象可以获得各种类型的资源;
ii. 在其他资源中,如布局文件中,引用资源格式@[包名称:]资源类型/资源名称
3.1 使用颜色资源color
新建res/values/colors.xml文件,内容如下:<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red_bg">#ff0000</color> <color name="blue_text">#0000ff</color> </resources> |
<TextView android:id="@+id/testtext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试颜色资源,红色背景,蓝色文字" android:textColor="@color/blue_text" /> |
TextView tv = (TextView) findViewById(R.id.testtext); tv.setBackgroundResource(R.color.red_bg); |
说明:
set BackgroundColor(0xFFFF0000);
set BackgroundColor(256);透明的
颜色6位,默认是透明的#FF00000
颜色8位,前两位代表透明度,值越小越透明
set BackgroundColor(R.id.colorname);透明的
set BackgroundResource(R.id.colorname);
3.2 使用字符串string资源
新建res/layout/values/strings.xml文件,添加两个字符串:<string name="text1">从资源文件引用string</string> <string name="text2">从代码中引用string</string> |
<TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text1" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> |
TextView tv1 = (TextView) findViewById(R.id.text1); TextView tv2 = (TextView) findViewById(R.id.text2); String str = getString(R.string.text2).toString(); tv2.setText(str);//得到实际字符串 //tv2.setText(R.string.text2);//通过id获得字符串 |
3.3使用尺寸dimen资源
尺寸单位有pix:像素,dp,sp ,mm:毫米,in:英尺等。控件尺寸推荐用 dp,和设备密度无关
文字尺寸推荐用sp,和设备精度无关
新建res/layout/values/dimens.xml尺寸文件,新建如下4个尺寸:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="text_width">150px</dimen> <dimen name="text_height">50px</dimen> <dimen name="btn_width">30mm</dimen> <dimen name="btn_height">10mm</dimen> </resources> |
<TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dimen测试" android:height="@dimen/text_height" android:width="@dimen/text_width" android:background="#00ff00"/> <Button android:id="@+id/btn_dimen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dimen测试2"/> |
Button btn = (Button) findViewById(R.id.btn_dimen); //方式一 btn.setWidth(this.getResources().getDimensionPixelSize(R.dimen.btn_width)); //方式二 btn.setHeight((int)this.getResources().getDimension(R.dimen.text_height)); |
3.4 使用assets资源
在assets下新建a.txt,添加内容hello 北京!在java文件中打印a.txt的内容:
AssetManager sm = this.getAssets(); String content = null; try { InputStream inStream = sm.open("a.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); //因为文件中只有一行,所以这里只读取了一行内容 content = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(content); |
点加号 --> 新建System.out过滤器查看输出日志:
说明:由于a.txt包含中文,会导致中文输出乱码,解决方法:将a.txt编码方式改为UTF-8即可
注意:自己的R文件与android.R文件
相关文章推荐
- Android学习——Android安装
- Android Studio格式化代码设置和代码风格设置、代码提示键
- HAL层开发框架介绍
- Android学习——Android入门
- android PendingIntent参数详细解析
- android入门——用RadioGroup实现Fragment的切换
- Android ListView EditText批量录入功能
- android aapt
- android Bitmap内存优化(三) 测量 ImageView 大小
- android 自定义属性
- Android中的进程机制(Binder)
- 如何使用Android命令
- Android Api Demos登顶之路(五十六)Service Remote Service
- @+id/android:list"的写法
- Android实战简易教程-第五十三枪(通过实现OnScrollListener接口实现上拉加载更多功能)
- android中的一些小技巧
- 【Android进阶】-Android ORM数据库框架 ActiveAndroid入门
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android核心基础-8.Android四大组件之Activity-3.生命周期、横竖屏切换、启动模式、进程管理
- 【转】android如何添加权限