第一个Android程序HelloWorld

来源:互联网 发布:b2b2c 分销 java 编辑:程序博客网 时间:2024/05/22 02:17

HelloWorld

写在前面

       一直是在看别人写的博客,从中收获了很多,也想过自己也写出很有深度的东西, 但是一直没有行动,一方面是不够自信,另一方面是懒吧(汗),不管怎么说,总算开始行动了,不说能写的东西对别人有多大的帮助,对我自己来说也算一种鞭策我学习的方式。

代码

<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" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Hello world!" /></RelativeLayout>

代码很简单,就是一个相对布局RelativeLayout里面嵌套了一个TextView,只不过Google推荐android:text="@string/hello_world"这么写,只不过这么写必须要在values/strings.xml中定义<string name="hello_world">Hello world!</string>,个人觉得这么写很麻烦,如果APP没有国际化的需求就不用这么写(个人意见,仅供参考 )

package com.anttaylor.hello;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

建立一个类继承Activity,在onCreat方法中调用setContentView设置布局

结果

这里写图片描述

0 0
原创粉丝点击