android实验四201306114104彭得源

创建工程

修改字符串资源

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

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

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

新建颜色资源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/blue" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textColor="@color/red"
        android:text_color="@color/red"/>
</RelativeLayout>

尺寸、布局资源的使用

创建工程

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

示例代码

package com.example.buju04;
import org.w3c.dom.Text;
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.chaxun);
        tv = (TextView)findViewById(R.id.gzcc);
        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;
    }
}

实验总结

学会了string的修改,和布局的修改等方法,亲自动手做实验效果好很多。

喜欢下次老师可以一边讲我们一边做,老师把演示窗口化,我们就可以跟着老师的步骤一边听一边做了。

posted on 2016-03-25 09:35  04彭得源  阅读(263)  评论(0编辑  收藏  举报