Android学习记录第一篇
2012-06-03 15:12
204 查看
建立HelloWorld工程,版本android4.0。
修改技巧:使用ALT+/ 来提示补全! 使用Ctrl+1 来建议修改错误!
学习内容:
1. gen目录下R.java文件是系统自动生成,默认有attr,drawble,layout,string 4个静态类,每次添加资源时会自动生成,这是UI与代码沟通的桥梁!java 代码中引用资源格式:R.resource_type.resource_name,xml布局文件中引用格式:@[package:]type/name,使用自己包下资源可省略包名,如访问android系统中资源时,包名android不可 省略。布局文件中需要给组件添加id时使用格式:@+id/string_name,这里‘+’表示在R.java文件的内部静态类中添加静态变量“string_name”,以使代码中根据id获获取相应组件。
2. layout与控件布局属性,layout_gravity是设置控件在容器内对齐方式。layout_width和layout_height设置填充方式,可设置为根据内容填充大小等。线性布局LineLayout里的控件排列方式设置(orientation属性):水平或者垂直排列。绝对坐标定义的Absolute布局,使用绝对坐标定位控件,AbsoluteLayout里面的控件都以layout_x
、layout_y来定义其位置。相对位置布局RelativeLayout,设置里面控件的排列属性。固定帧方式布局FrameLayout,布局里所有的控件都被放到布局的左上角,一层覆盖一层。
3. 控件与变量的绑定以及控件事件的监听。
修改main.xml如下:
修改HelloWorldActivity.java如下
运行效果:
修改技巧:使用ALT+/ 来提示补全! 使用Ctrl+1 来建议修改错误!
学习内容:
1. gen目录下R.java文件是系统自动生成,默认有attr,drawble,layout,string 4个静态类,每次添加资源时会自动生成,这是UI与代码沟通的桥梁!java 代码中引用资源格式:R.resource_type.resource_name,xml布局文件中引用格式:@[package:]type/name,使用自己包下资源可省略包名,如访问android系统中资源时,包名android不可 省略。布局文件中需要给组件添加id时使用格式:@+id/string_name,这里‘+’表示在R.java文件的内部静态类中添加静态变量“string_name”,以使代码中根据id获获取相应组件。
2. layout与控件布局属性,layout_gravity是设置控件在容器内对齐方式。layout_width和layout_height设置填充方式,可设置为根据内容填充大小等。线性布局LineLayout里的控件排列方式设置(orientation属性):水平或者垂直排列。绝对坐标定义的Absolute布局,使用绝对坐标定位控件,AbsoluteLayout里面的控件都以layout_x
、layout_y来定义其位置。相对位置布局RelativeLayout,设置里面控件的排列属性。固定帧方式布局FrameLayout,布局里所有的控件都被放到布局的左上角,一层覆盖一层。
3. 控件与变量的绑定以及控件事件的监听。
修改main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:text="EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/edtInput"/> <LinearLayout android:id="@+id/lineLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center_horizontal"> <Button android:id="@+id/btnShow" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="显示"/> <Button android:id="@+id/btnClear" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="清除"/> </LinearLayout> </LinearLayout>
修改HelloWorldActivity.java如下
package com.zombie.hello; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ Button btnShow; Button btnClear; EditText edtInput; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 控件与变量的绑定 btnShow = (Button)findViewById(R.id.btnShow); btnClear = (Button)findViewById(R.id.btnClear); edtInput = (EditText)findViewById(R.id.edtInput); // 添加按钮事件响应 btnShow.setOnClickListener(new ClickListener()); btnClear.setOnClickListener(new ClickListener()); } class ClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub if( v == btnShow ) { new AlertDialog.Builder(HelloWorldActivity.this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("信息") .setMessage(edtInput.getText()) .show(); } else if( v == btnClear ) { edtInput.setText("Hello world, android!"); } } } }
运行效果:
相关文章推荐
- 记录一下Android大神们的博客地址,学习之路漫漫啊
- Android 学习记录-基础控件与布局
- android&&ruby on rails学习记录
- Android学习记录(十一) 填坑之inputStream close关闭慢的处理。
- 第一个建立的博客,记录学习android的点点滴滴
- Android学习记录之:Toast的使用
- 【转】Android使用SQLiteOpenHelper对数据库进行版本管理【学习记录】
- 第一篇博文,记录自己的研究生学习历程
- android:listView学习记录2
- Android学习记录 - 界面布局
- osg android学习--经验记录
- 深入理解Android的消息处理系统---Handler、Looper、Thread学习记录
- Android编程学习系列:第一篇,搭建开发平台
- 【android学习记录】listview动画效果
- Android 热更新学习记录
- Android-JNI NDK的学习记录
- android学习记录
- Android学习记录20171023--Volley网络框架(一)
- Android Gradle学习记录6 代码记录(持续补充)
- android 学习记录-----------android 活动 意图 碎片