android快速上手(二)android开发环境搭建及hello world

来源:互联网 发布:四宫格视频是什么软件 编辑:程序博客网 时间:2024/05/01 23:10

基本了解了java语法,下一步,我们一起开启hello world的神秘之旅。


(一)android开发环境搭建

之前搭建android开发环境是件非常费力的事情,下载Eclipse,安装ADT等,如今android官方给我们提供了全套配置。

https://developer.android.com/sdk/index.html

搭建android开发环境之前记得先安装jdk


(二)开启Hello World之旅

(1)创建Hello World项目

安装完带ADT的Eclipse,打开Eclipse,创建新项目

一路next和finish下去,最后生成项目如下

不同版本创建的项目,生成的内容会不同,俺用的是4.4版本的SDK

运行项目


(2)项目结构分析

生成项目结构较为复杂,想深入了解的同学可以继续看,也可以暂时略过。

1.drawable目录存在图片

android支持不同分辨率手机图片适配,对应图片放在对应的文件夹,图片一般放于drawable-hdpi,图片的xml放于drawable中

2.layout目录存在布局

布局即显示的UI,同样支持不同的分辨率和横竖屏专门适配

3.values目录存在数值资源信息

color对应颜色值,string对应字符串,dimens对应尺寸,styles对应主题样式、menu存放菜单信息等

4.AndroidManifest.xml文件

声明Activity、Service、Broadcast等信息,设置app能使用的权限、包名、版本信息等

5.gen文件夹

保存自动生成的、位于android项目包下的R.java文件,R文件存放资源信息映射值,程序中直接调用

6.libs文件夹

存放第三方调用的lib


(3)hello wolrd深入

要使用控件,需拿到控件,拿控件通过R中的控件id值

在fragment_main.xml中添加helloworld文本的id值

<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="com.peter.demo.helloworld.MainActivity$PlaceholderFragment" >    <TextView        android:id="@+id/hello_world"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>


拿到TextView对象,对它进行显示赋值

在MainActivity.java中
/**     * A placeholder fragment containing a simple view.     */    public static class PlaceholderFragment        extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container, false);            TextView tv = (TextView) rootView.findViewById(R.id.hello_world);            tv.setText("hello world!");            return rootView;        }    }

(4)helloworld扩展

完成了helloworld,下面让我们一起继续玩转,千变万化的Helloworld。


1.面向对象的helloworld

创建HelloWorld对象

/** * helloworld对象 *  * @author peter_wang * @create-time 2014-5-11 上午10:37:04 */public class HelloWorld {    private String mText;    public HelloWorld() {        this.mText = "Hello World!";    }    public String getmText() {        return mText;    }    public void setmText(String mText) {        this.mText = mText;    }}

修改MainActivity.java中TextView部分

TextView tv = (TextView) rootView.findViewById(R.id.hello_world);HelloWorld helloWorld = new HelloWorld();tv.setText(helloWorld.getmText());


2.修改Helloworld显示样式

TextView tv = (TextView) rootView.findViewById(R.id.hello_world);tv.setText("hello world!");//设置显示颜色为绿色tv.setTextColor(Color.GREEN);//设置字体大小tv.setTextSize(18);//加粗TextPaint paint =  tv.getPaint();  paint.setFakeBoldText(true); 








(三)学习概要

开发环境搭建较容易,helloworld创建项目自动生成,熟悉下整个项目的结构,感兴趣的同学自己发挥创意改下代码,写代码一定要乐在其中,一切在自己的掌握中,创建自己的小东西。


0 0
原创粉丝点击