Chinaunix首页 | 论坛 | 博客
  • 博客访问: 329240
  • 博文数量: 76
  • 博客积分: 180
  • 博客等级: 入伍新兵
  • 技术积分: 193
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-04 10:36
个人简介

E_mail:czqwust@163.com 专注于linux、嵌入式

文章分类

全部博文(76)

文章存档

2016年(1)

2015年(38)

2014年(12)

2012年(25)

我的朋友

分类: LINUX

2012-09-07 22:29:40

作为Android入门篇,我先从设计Android的UI开始,然后添加几个控件,已达到对开发Android程序的大致了解。

1.设计Android程序的UI

在Android中,应用程序的界面设计都是使用XML语言设计的,在Activity函数中,函数setContentView()来设计使用什么界面设计,默认情况下,我们使用的是main.xml。

在main.xml中,我是用了两个线性布局(Linearlayout),第一个应用于整个程序的布局,第二个应用于控件的布局。

main.xml


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="@string/hello" />
  10.     <EditText android:text="EditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editinput"></EditText>
  11.     <LinearLayout android:id="@+id/LinearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center">
  12.         <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:id="@+id/buttonshow" ></Button>
  13.         <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:id="@+id/buttonclear" ></Button>
  14.     </LinearLayout>

2.main.xml只是设计了界面,对于应用程序的功能,我们需要在Activity中实现。

我们在Activity中设计了两个按钮,一个文本输入框,首先要做的是建立相应的对象,然后通过ID查找到控件,为了让两个个按钮在按下的时候有反应,我们为这两个按钮设置了监听器,通过弹出对话框的方式显示监听器的作用。

Activity中的部分代码:


点击(此处)折叠或打开

  1. Button buttonshow;
  2.    Button buttonclear;
  3.    EditText edittext;
  4.     public void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  7.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  8.         setContentView(R.layout.main);
  9.         buttonshow=(Button)findViewById(R.id.buttonshow);
  10.         buttonclear=(Button)findViewById(R.id.buttonclear);
  11.         edittext=(EditText)findViewById(R.id.editinput);
  12.         buttonshow.setOnClickListener(new ClickListener());
  13.         buttonclear.setOnClickListener(new ClickListener());

3.全屏显示应用程序

为了在使用程序的时候全屏显示,我们只需要使用下面的两句代码就可以了,但是要在setContentView()函数之前。


点击(此处)折叠或打开

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);
  2. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

程序效果图

阅读(2934) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~