当前位置:文档之家› 安卓实验报告二

安卓实验报告二

洛阳理工学院实验报告
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"执行了onPause()方法");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"执行了onRestart()方法");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"执行了onDestroy()方法");
}
}
2.在assets中保存一副图片,并在Activity中显示出来,程序运行结果如下:
package com.example.lenovo.shiyan.char03;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.example.lenovo.shiyan.R;
import java.io.IOException;
import java.io.InputStream;
public class Assets_ActivityDemo extends AppCompatActivity {
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(yout.assets_layout);
iv = (ImageView)findViewById(R.id.im1);
try{
InputStream is = getResources().getAssets().open("2.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bitmap);
}catch (IOException e){
e.printStackTrace();
} }}
3.使用string.xml文件设置字符串的内容,使用dimens.xml文件设置字符串大小,使用color.xml文件设置字符串的颜色,使用style.xml文件设置字符串的样式,然后在Activity中显示字符串,程序运行结果如下图所示:
package com.example.administrator.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Dimen_ActivityDemoActivity extends AppCompatActivity {
TextView tv1,tv2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_dimen__demo);
tv1=(TextView)findViewById(1);
tv1.setTextSize(getResources().getDimension(R.dimen.dimen_xml));
tv1.setTextColor(getResources().getColor(R.color.color_xml));
tv1.setText(R.string.app_xml);
tv1.setTextAppearance(this,R.style.blue_textview);
tv2 =(TextView)findViewById(2);
tv2.setTextSize(getResources().getDimension(R.dimen.dimen_java));
tv2.setTextColor(getResources().getColor(R.color.color_java));
tv2.setText(R.string.app_class);
tv2.setTextAppearance(this,R.style.red_textview);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_xml"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="@dimen/dimen_xml"
android:textColor="@color/color_xml"
style="@style/blue_textview"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_class"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/color_java"
style="@style/red_textview"/>
</LinearLayout>
实验总结:本次实验书上都有说明,所以不是很难,按照书上的思路进行分析跟操作就能够得出结果,虽然不难,但是看书仍然会存在一些问题,比如文件的位置,名称问题都会影响代码的正确性。

相关主题