参考《Professional Android 4 Development》

Android UI基本元素

下面这些概念是Android UI设计的基础,深入学习和理解它们是Android UI设计的基础:

  • View:View是所有UI元素,包括Layout在内,的父类。
  • View Groups:View的子类,实现了ViewManager接口。一个ViewGroup可以包含多个子View。
  • Fragmengts:用于封装UI的基本元素。Fragment有自己的layout配置文件,可以接收用户输入,可以方便地适配不同的屏幕。
  • Activity:用于表达android设备的屏幕,Activity类似于Web开发中的Form表单。View等UI元素只有绑到Activity中才可以被用户可见。

Android UI基础

Android UI与Activity的绑定

最常用的方法是使用setContentView()将Layout ID或View对象传到Activity中,例如:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
}
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView myTextView = new TextView(this);
  setContentView(myTextView);

  myTextView.setText(“Hello, Android”);
}

同样的,使用findViewById()方法可以通过View ID获取View对象:

TextView myTextView = (TextView)findViewById(R.id.myTextView);

Layout简介

Layout是ViewGroup的子类,用于描述和管理Android UI的布局。Android自带了很多Layout,常用的包括FrameLayout,LinearLayout,RelativeLayout和GridLayout。关于Layout的更多信息,可以参考这个链接:

http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts

下面是一个Layout示例文件:

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
  <TextView
  android:layout_width=”match_parent”
  android:layout_height=”wrap_content”
  android:text=”Enter Text Below”
  />
  <EditText
  android:layout_width=”match_parent”
  android:layout_height=”wrap_content”
  android:text=”Text Goes Here!”
  />
</LinearLayout>

wrap_content将View的高(或宽)设置为能包裹控件内容的最小值,而match_parent则使View尽可能地填充父容器。

Layout优化

<merge>: 由于Layout可以nest(叠加?)使用,所以有时会出现冗余的Layout。一个常见的例子是使用FrameLayout创建一个单根的配置文件,例如:

<?xml version=”1.0” encoding=”utf-8”?>
<FrameLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
  <ImageView
  android:id=”@+id/myImageView”
  android:layout_width=”match_parent”
  android:layout_height=”match_parent”
  android:src=”@drawable/myimage”
  />
  <TextView
  android:id=”@+id/myTextView”
  android:layout_width=”match_parent”
  android:layout_height=”wrap_content”
  android:text=”@string/hello”
  android:gravity=”center_horizontal”
  android:layout_gravity=”bottom”
  />
</FrameLayout>

若将上面的layout添加到另一个Layout中,则会产生冗余(redundant),更好的解决方式是使用<merge>标签:

<?xml version=”1.0” encoding=”utf-8”?>
<merge xmlns:android=”http://schemas.android.com/apk/res/android”>
  <ImageView
  android:id=”@+id/myImageView”
  android:layout_width=”match_parent”
  android:layout_height=”match_parent”
  android:src=”@drawable/myimage”
  />
  <TextView
  android:id=”@+id/myTextView”
  android:layout_width=”match_parent”
  android:layout_height=”wrap_content”
  android:text=”@string/hello”
  android:gravity=”center_horizontal”
  android:layout_gravity=”bottom”
  />
</merge>

当使用<merge>标签的配置文件被嵌入到另一个文件中时,<merge>标签会被删掉。<merge>标签常和<include>一起使用,例如:

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
  <include android:id=”@+id/my_action_bar”
   layout=”@layout/actionbar”/>
  <include android:id=”@+id/my_image_text_layout”
   layout=”@layout/image_text_layout”/>
</LinearLayout>

使用ViewStub

ViewStub是View的子类,使用类似Lazy Load的方式加载,从而节省了系统资源。在代码中使用:

// Find the stub
View stub = findViewById(R.id. download_progress_panel_stub);
// Make it visible, causing it to inflate the child layout
stub.setVisibility(View.VISIBLE);
// Find the root node of the inflated stub layout
View downloadProgressPanel = findViewById(R.id.download_progress_panel);

配置文件:

<?xml version=”1.0” encoding=”utf-8”?>
<FrameLayout “xmlns:android=http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
  <ListView
  android:id=”@+id/myListView”
  android:layout_width=”match_parent”
  android:layout_height=”match_parent”
  />
  <ViewStub
  android:id=”@+id/download_progress_panel_stub”
  android:layout=”@layout/progress_overlay_panel”
  android:inflatedId=”@+id/download_progress_panel”
  android:layout_width=”match_parent”
  android:layout_height=”wrap_content”
  android:layout_gravity=”bottom”
  />
</FrameLayout>

Android 4学习(7):用户界面 - 基础的更多相关文章

  1. Android Ormlite 学习笔记1 -- 基础

    Ormlite 是一个开源Java数据实体映射框架.其中依赖2个核心类库: 1.ormlite-android-4.48.jar 2.ormlite-core-4.48.jar 新建项目,引用上面2个 ...

  2. Android开发学习路线图

    Android开发学习方法: Android是一个比较庞大的体系,从底层的Linux内核到上层的应用层,各部分的内容跨度也比较大.因此,一个好的学习方法对我们学习Android开发很重要. 在此建议, ...

  3. Android:日常学习笔记(7)———探究UI开发(4)

    Android:日常学习笔记(7)———探究UI开发(4) UI概述  View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...

  4. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  5. Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例

    ---------------------------------------------------------------------------------------------------- ...

  6. Android Testing学习02 HelloTesting 项目建立与执行

    Android Testing学习02 HelloTesting 项目建立与执行 Android测试,分为待测试的项目和测试项目,这两个项目会生成两个独立的apk,但是内部,它们会共享同一个进程. 下 ...

  7. Android Testing学习01 介绍 测试测什么 测试的类型

    Android Testing学习01 介绍 测试测什么 测试的类型 Android 测试 测什么 1.Activity的生命周期事件 应该测试Activity的生命周期事件处理. 如果你的Activ ...

  8. Android动画学习笔记-Android Animation

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

  9. Android开发学习总结(一)——搭建最新版本的Android开发环境

    Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...

  10. Android:学习AIDL,这一篇文章就够了(下)

    前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...

随机推荐

  1. 一个专为电商定制的域名.shop

    2.73亿元人民币获得.shop域名的经营权,使shop域名成为最高节拍价的顶级域名.虽然最终“最高节拍价”被web域名打破,但在电商届域名里shop还是王者.shop作为一个主要面向线上.线下销售实 ...

  2. Delphi 的 Utf-8 转换

    新版的 Delphi 應該不用這麼麻煩, 據說只要直接在 AnsiString, WideString, UTF8String 之間 assign 時就會自動幫你做轉換 (沒用過, 不知道是不是真的這 ...

  3. struts2学习(4)

    Struts2拦截器概述 1 Struts2是框架,封装了很多功能,struts2里面封装的概念都是在拦截器里面 2 Struts2里面封装了很多的概念,有很多拦截器,不是每次这些拦截器都执行,每次执 ...

  4. jQuery download file

    jQuery.download = function (url, method, p, c, e, i, o, goodsType, reciveUser, suplier) { jQuery('&l ...

  5. CSS基础(滑动门,雪碧图,网页布局)

    3.11.2017 这一篇主要是关于滑动门技术的学习,还有雪碧图(sprite),也就是精灵图,还有一点昨天的css可见性的回顾,下面先来回顾下吧 CSS可见性(元素可见性) overflow: hi ...

  6. window.name 跨域数据传输

    通过window.name可以实现跨域数据传输. 要解决的功能:  www.a.com/a.html 需要获取到 www.b.com/b.html页面内容的数据 需要3个页面 www.a.com/a. ...

  7. shell脚本:利用7z备份git项目

    注:无git的方法参见:tar 或 7z 备份项目 首先利用homebrew安装p7zip $ brew install p7zip 然后利用两个shell脚本: backupProject.sh 会 ...

  8. JDK自动安装脚本

    A:本脚本运行的机器,Linux B:待安装JDK的机器, Linux 首先在脚本运行的机器A上确定可以ssh无密码登录到待安装jdk的机器B上,然后就可以在A上运行本脚本: 代码如下: $ ./in ...

  9. POJ 3071 Football (概率DP)

    概率dp的典型题.用dp[j][i]表示第j个队第i场赢的概率.那么这场要赢就必须前一场赢了而且这一场战胜了可能的对手.这些都好想,关键是怎么找出当前要算的队伍的所有可能的竞争对手?这个用异或来算,从 ...

  10. 转:STL迭代器失效问题

    . 对于关联容器(如map, set, multimap,multiset),删除当前的iterator,仅仅会使当前的iterator失效,只要在erase时,递增当前iterator即可.这是因为 ...