实验3 颜色、字符串资源使用

实验报告

课程名称

基于Android平台移动互联网开发

实验日期

2016年3月4日

实验项目名称

颜色、字符串资源的使用

实验地点

S30010

实验类型

□验证型    √设计型    □综合型

学  时

2

一、实验目的及要求(本实验所涉及并要求掌握的知识点)

1.目的:掌握Android中颜色和字符串资源的使用方法。

理解Android中尺寸和布局资源的使用方法。

2.要求:在工程中为Activity、View使用颜色资源;

使用字符串资源并理解字符串资源的引用机制;

为Android中的视图组件设定尺寸;

运用布局资源将界面上的组件放在指定的位置。

二、实验环境(本实验所使用的硬件设备和相关软件)

(1)PC机

(2)操作系统:Windows XP

(3)软件: Eclipse, JDK1.6,Android SDK,ADT

三、实验内容及步骤

  1. 创建工程
  2. 修改字符串资源
  3. 新建颜色资源color.xml
  4. 使用资源颜色和字符串资源
  5. 尺寸、布局资源的使用

四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)

代码:

Resdemo 的strings.xml的代码:

<resources>

 

    <string name="app_name">Resdemo</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

 

</resources>

Resdemo的activity_main.xml的部分代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity"

    android:background="@color/blue_bg">

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world"

        android:textColor="@color/red_bg"

        android:textSize="30sp" />

 

</RelativeLayout>

Resdemo的MainActivity.java的代码:

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity {

      @Override

      protected void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.activity_main);

      }

      @Override

      public boolean onCreateOptionsMenu(Menu menu) {

             // Inflate the menu; this adds items to the action bar if it is present.

             getMenuInflater().inflate(R.menu.main, menu);

             return true;

      }

}

Ziyuan的activity_main.xml的部分代码:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity"

    android:background="@drawable/ic_launcher" >

<LinearLayout

    android:id="@+id/linearLayout1"

    android:layout_width="350dp"

    android:layout_height="wrap_content"

    android:orientation="vertical">

   

 

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:text="@string/gzcc"

        android:textSize="50dp"/>

</LinearLayout>

 

<Button

    android:id="@+id/button1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_below="@+id/linearLayout1"

    android:layout_centerHorizontal="true"

    android:layout_marginTop="54dp"

    android:text="@string/btn"

     android:textSize="40dp" />

   

</RelativeLayout>

Ziyuan的MainActivity.java的代码:

package com.example.ziyuan;

import android.os.Bundle;

import android.app.Activity;

import android.content.DialogInterface.OnClickListener;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv;

    Button btn;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       btn=(Button)findViewById(R.id.button1);

       tv=(TextView)findViewById(R.id.textView1);

       btn.setOnClickListener(new View.OnClickListener(){

          public void onClick(View v){

              tv.setTextColor(TRIM_MEMORY_MODERATE);

          }

       });

      

   }

   @Override

  

   public boolean onCreateOptionsMenu(Menu menu) {

       // Inflate the menu; this adds items to the action bar if it is present.

       getMenuInflater().inflate(R.menu.main, menu);

       return true;

   }

}

运行结果:(截图)

1. Resdemo项目效果图:

2. Ziyuan项目效果图:

 

五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)

通过这一次的实验,我对字符串、颜色的使用更加的了解,不会像以前一样盲目,不知道如何下手,希望老师不仅仅会布置新的作业,也布置一点让我们把知识巩固好的作业,让我们对这门课程更加地熟悉以及热爱。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           
原文地址:https://www.cnblogs.com/xy1015/p/5340697.html