zoukankan      html  css  js  c++  java
  • android高速上手(二)android开发环境搭建及hello world

    基本了解了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); 








    (三)学习概要

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


  • 相关阅读:
    源码阅读(18):Java中主要的Map结构——HashMap容器(中)
    源码阅读(17):红黑树在Java中的实现和应用
    java中创建文件并写入的方法
    源码阅读(16):Java中主要的Map结构——HashMap容器(上)
    源码阅读(15):Java中主要的Map结构——概述
    从源码分析:Java中的AQS
    源码阅读(14):Java中主要的Queue、Deque结构——PriorityQueue集合(下)
    源码阅读(13):Java中主要的Queue、Deque结构——PriorityQueue集合(中)
    java 归并排序与快速排序
    python中的容器、可迭代对象、迭代器、生成器
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5210715.html
Copyright © 2011-2022 走看看