您的位置:首页 > 移动开发 > Android开发

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"
/>

activity_second.xml:

<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>

MainActivity.java:

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);
}
}

SecondActivity需要在清单文件中注册(AndroidMainfest.xml):

<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);
}

View 的可见性分为三种:

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;
}
}

在第一个Activity中设置score的值为100:

MyApplication app = (MyApplication) this.getApplication();
app.setScore(100);

在第二个Activity中取出score的值:

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>

在activity_main.xml布局文件中添加一个TextView,设置其文字颜色:

<TextView
android:id="@+id/testtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试颜色资源,红色背景,蓝色文字"
android:textColor="@color/blue_text"
/>

在MainActivity.java中设置TextView的背景色:

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,一个通过@string/text1设置值,另一个在java文件中设置:

<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=""
/>

在java文件中:

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设置尺寸大小:

<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"/>

在java文件中设置Button的尺寸:

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文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: