电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> object-c>>eclipse中安卓开发项目layouts的作用:Android用户界面(布局):

eclipse中安卓开发项目layouts的作用:Android用户界面(布局)

来源:网络 | 2012-12-22 | (有6221人读过)

Android 用户界面或布局layouts,  
是通过XML文档来描述的,可以在项目的res/layouts目录下找到。在之前运行在模拟器上代码中,我们可以看到由eclipse自动生成的布局代码在res/layouts/main.xml  
中。 


Eclipse有一个图形化的布局设计器,通过在屏幕上的拖拽控制来完成布局的设计,然而,我却发现直接写XML并使用图形布局来预览是更容易的方式。 


现在让我们对main.xml做一些工作以达到上图的效果: 



在Eclipse中通过双击PackageExplorer的res/layouts/main.xml 来打开xml。  
点击屏幕下方main.xml 来切换为xml视图。 

将main.xml中内容改为如下的内容:
# /res/layouts/main.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">  
  <LinearLayout  
    android:orientation="horizontal"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:padding="10dip">  
    <TextView  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:textSize="20dip"  
      android:text="Brews: " />  
    <TextView  
      android:layout_width="fill_parent"  
      android:layout_height="wrap_content"  
      android:text="None"  
      android:gravity="right"  
      android:textSize="20dip"  
      android:id="@+id/brew_count_label" />  
  </LinearLayout>  
  <LinearLayout  
    android:orientation="horizontal"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:layout_weight="1"  
    android:gravity="center"  
    android:padding="10dip">  
    <Button  
      android:id="@+id/brew_time_down"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="-"  
      android:textSize="40dip" />  
    <TextView  
      android:id="@+id/brew_time"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="0:00"  
      android:textSize="40dip"  
      android:padding="10dip" />  
    <Button  
      android:id="@+id/brew_time_up"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="+"  
      android:textSize="40dip" />  
  </LinearLayout>  
  <Button  
    android:id="@+id/brew_start"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom"  
    android:text="Start" />  
</LinearLayout>
正如你所见的,Android的XML布局文件是繁琐的,但却能让你控制到屏幕的各个元素。 


在Android中最重要的接口元素是布局Layout容器,例如例子中使用的LinearLayout  
。这些元素对于用户是不可见的,但是却扮演者例如Buttons 和TextViews这些元素的布局容器。 


Android中有几种不同类型的布局视图layout view,每一种都用于开发不同的布局。如同LinearLayout 和AbsoluteLayout  
,TableLayout 可以让你使用更为复杂的基于表格结构的布局。你可以在SDK的API文档的通用布局对象中查找到更多的布局。
object-c热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号