Android学习——界面编程!
来源:互联网 发布:淘宝上真正的原单包店 编辑:程序博客网 时间:2024/05/22 14:22
From:http://blog.csdn.net/zjfstudio/article/details/4215219
在Android系统中,组成界面的元素主要有:
- Views:所有用户界面展示信息的基类,包括通用控件,自定义控件,就相当于C#编程中的Controls;
- ViewGroups:是Views的继承类,支持多个控件进行组合,有点象C#编程的UserControls;
- Activities:是窗体的基类,相当于C#中的Form。
看个类图:
常用的控件有:TextView、EditText、ListView、Spinner(相当于Combox)、Button、CheckBox、RadioButton等等。控件比较多,以后可以边用边学,跟C#界面控件还差不多,下面我们就看看用法吧,现在再回头看看我们的Hello World程序:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
这里,setContentView(R.layout.main);就是把我们定义在resource中的控件加载进来,同样,换成程序写法:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView myTextView = new TextView(this);
setContentView(myTextView);
myTextView.setText(“Hello, Android”);
}
看到这段代码,大家可能就会想到一个控件位置,在C#编程中,我们在一个Form中会定义一个界面里,会将多个控件加载到主窗体中,然后定义好坐标(x,y)即可。
在Android界面中,控件布局有5种:
- FrameLayout:层叠,新加的控件会叠加到原控件上面,遮挡。
- LinearLayout:直线添加控件,可垂直,可水平,依次排开部署控件,通过android:orientation属性来改变方向。
- RelativeLayout:相对布局,相对一个控件的ID,如例如toLeft:”okbutton”, below:”text”。
- TableLayout:表格布局,用table的行列来定位控件。
- AbsoluteLayout:绝对位置,用x,y来定位。
使用布局:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView myTextView = new TextView(this);
EditText myEditText = new EditText(this);
myTextView.setText(“Enter Text Below”);
myEditText.setText(“Text Goes Here!”);
int lHeight = LinearLayout.LayoutParams.FILL_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
ll.addView(myTextView, new LinearLayout.LayoutParams(lHeight, lWidth));
ll.addView(myEditText, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
或用xml:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Enter Text Below”
/>
<EditText
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Text Goes Here!”
/>
</LinearLayout>
开发自定义控件:
在用了己有控件后,我们就想自己开发自定义控件,当然开发很简单,只要继承一下View类或者是继承继承View类的现有控件。
public class CompassView extends View{
protected void onDraw(Canvas canvas) {
//绘制, 这里的canvas就想当于C#中的graphics,能画点线面。
}
}
消息处理可以用对应的虚函数,如:
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
...处理按钮消息
postInvalidate();//用于重绘界面
}
好了,有了这些基本方法也就可以开始我们的界面之旅了。
- Android学习——界面编程!
- Android学习——界面编程!
- android学习——应用界面编程
- Android学习——Android界面UI
- Android应用界面编程 ImageView学习
- Android基础知识学习——界面转换
- Android——动画学习,界面切换
- Android界面编程——Android基本控件
- Android界面编程——Android布局组件(二)
- Android界面编程——Android高级组件
- [python]python学习笔记(六)——界面编程
- Android应用的界面编程——View类
- Android界面编程——对话框控件(四)
- Android界面编程——日期时间组件(五)
- 初步学习界面编程
- android基本界面编程
- Android界面编程
- Android应用界面编程
- oracle用户访问限制相关
- java对象与xml文件转换
- magento 通过命令行index的方法
- android 开发 短信发送器
- JAVA WEB开发----国际化
- Android学习——界面编程!
- Activemq配置——Jaas方式配置用户登录验证
- c++关键字之static
- VC++ 6.0 转VS2005以上版本(2)
- JDBC工具类
- 点击父分类前的图标时能够显示、折叠子分类。
- AndroidManifest中original-package标签
- 在内存中装Win7
- ARM DUI 读书笔记引用