【目的】

  1. 掌握Android中颜色和字符串资源的使用方法。
  2. 理解Android中尺寸和布局资源的使用方法。

【要求】

  1. 在工程中为Activity、View使用颜色资源;
  2. 使用字符串资源并理解字符串资源的引用机制;
  3. 为Android中的视图组件设定尺寸;
  4. 运用布局资源将界面上的组件放在指定的位置。

 

【原理】

Android资源管理机制。

【过程】

1.  创建工程

 

 

                        New Android Project 视图

 

           Resdemo project 视图

  2. 修改字符串资源

打开/res/values/strings.xml文件。

                         Strings.xml视图

点击Add按钮,添加字符串,输入字符串的name 和value

新建的工程含有三个已有字符串

3. 新建颜色资源color.xml

 

                                          Color.xml 视图

颜色:R.color.red_gb

字符串:R.strings.s

打开res/layout/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="@color/qinglv">
    <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>

于是,界面变成了这样

我们让TextView控件显示指定的字符串,并使用red_bg颜色资源,背景使用qinglv资源。

3. 尺寸、布局资源的使用

【过程】

1、 创建工程

a) Project name:Ziyuan

b) Package name:com.example.Ziyuan

c) Activity name: MainActivity

d) Application name::Ziyuan

e) Min SDK Version:8

                                      创建工程图

                          Ziyuan project 视图

2.打开activity_main_xml开始布局以一个Button按钮,一个TextView为例

<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"
    android:background="@drawable/ic_launcher"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="16dp"
        android:text="@string/btn" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="@string/tv"
        android:textSize="20dp" />

</RelativeLayout>

  布局文件图 

                                         布局后的效果图

3.示例代码

package com.example.ziyuan;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
    private Button btn;
    private TextView tv;
    @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 OnClickListener() {
            
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                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;
    }
}

思考并解答:

表示大小、尺寸的单位各有哪些?分别应用在什么场合?

px:表示屏幕实际的象素。例如,320*480的屏幕在横向有320个象素,在纵向有480个象素。

in:表示英寸,是屏幕的物理尺寸。每英寸等于2.54厘米。例如,形容手机屏幕大小,经常说,3.2(英)寸、3.5(英)寸、4(英)寸就是指这个单位。这些尺寸是屏幕的对角线长度。如果手机的屏幕是3.2英寸,表示手机的屏幕(可视区域)对角线长度是3.2*2.54 = 8.128厘米。读者可以去量一量自己的手机屏幕,看和实际的尺寸是否一致。

mm:表示毫米,是屏幕的物理尺寸。

pt:表示一个点,是屏幕的物理尺寸。大小为1英寸的1/72。

px(Pixels ,像素):对应屏幕上的实际像素点。例如,320*480的屏幕在横向有320个象素,在纵向有480个象素。

in(Inches ,英寸):屏幕物理长度单位。每英寸等于2.54厘米。例如,形容手机屏幕大小,经常说,3.2(英)寸、3.5(英)寸、4(英)寸就是指这个单位。这些尺寸是屏幕的对角线长度。如果手机的屏幕是3.2英寸,表示手机的屏幕(可视区域)对角线长度是3.2*2.54 = 8.128厘米。读者可以去量一量自己的手机屏幕,看和实际的尺寸是否一致。

mm(Millimeters ,毫米):屏幕物理长度单位。

pt(Points ,磅):屏幕物理长度单位, 表示一个点,是屏幕的物理尺寸。大小为1英寸的1/72。

dp(与密度无关的像素):逻辑长度单位,在 160 dpi 屏幕上,1dp=1px=1/160英寸。随着密度变化,对应的像素数量也变化,但并没有直接的变化比例。

dip:与dp相同,多用于Google示例中。

sp(与密度和字体缩放度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放。

尽量使用dp作为空间大小单位,sp作为和文字相关大小单位。

实验总结

通过这次实验,我初步掌握了Android中颜色和字符串资源的使用方法,也深入理解了Android中尺寸和布局资源的使用方法。

在使用颜色资源时因为大意将RGB值漏打了一个#号,所以程序报错,不过通过认真检查后发现了问题后,添加上了;

初步引用字符串资源的时候还不熟练,一开始出错很多,后来渐渐上手了些;

通过对程序代码的一些数值进行了修改,进一步理解了如何为Android中的视图组件设定尺寸;

运用布局资源将界面上的组件放在指定的位置,然后再对照代码,深入理解布局的设置。

 

posted on 2016-03-27 16:45  36张文雅  阅读(267)  评论(0编辑  收藏  举报