出处:http://blog.csdn.net/android_tutor/article/details/5995759

大家好,今天给大家分享的是Android中UI设计的一些技巧,本节内容主要有两点:一是Android按钮(Button)的UI设计,二是:ListView以及GridView的UI设计。

按钮的状态:

我们一般搞UI设计,按钮通常有三个状态:normal(正常状态);focus(焦点状态),pressed(按下状态)。如下图所示:

                                         

我们会在res/drawable目录下定义一个资源文件,比如我们本例中要用到的handle.xml,在里面定义三种状态,每种状态对应一张图片:

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_window_focused="false"  android:drawable="@drawable/handle_normal" />
  4. <item android:state_focused="true" android:drawable="@drawable/handle_focused" />
  5. <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
  6. </selector>

而我们使用这个资源文件的用法只需要引用drawable里的资源文件(android:background="@drawable/handle")代码如下:

  1. <Button
  2. android:id="@+id/handle"
  3. android:layout_width="wrap_content"
  4. android:layout_height="fill_parent"
  5. android:background="@drawable/handle"
  6. />

Android中的层:

看过《盗梦空间》的人都知道,梦境有多少层,而Android中也有层次之分,在Android中第一层"梦境",我们可以认为是壁纸。第二层就是应用的Activity,第三层就是放在Activity上的容器(ViewGroup以及它的子类FrameLayout,LinearLayout等布局对象),当然容器中还可以放容器,你也可以放到N层(最多放多少我还没验证过),总之最后一层就是那些继承于View的控件了(诸如,Button,TextView等.)

而ListView以及GridView中UI是怎么设计的呢,下面我们看一下效果图:

上图是一个ListView的效果图,正常状态下是白色背景黑色字体,当我们点击一列时会出现黄色背景。这一效果是如何做到的呢?

ListView单元格显示的内容其实是我们事先定义在Layout目录下的一个布局文件,从这个效果来看,我们可以看出它一共有三个“层”

第一层容器(LinearLayout) 背景色为白色:

第二层也是容器(LinearLayout)当按下时,背景色为黄色,把第一层挡住(具体做法可以参照按钮):

第三层是控件(TextView)。

实例 :

上面说了一些,有些人肯定会云里雾里,所以我们直接来个实例,实例做完后,再看一下,效果会更好,大家按照步骤跟我来:

第一步:首先准备素材,准备三个按钮,以及ListView的背景图(上面三个按钮已经有了,下面我只贴一个ListView背景图片):

第二步:新建一个Android工程,命名为UIDemo.目录结构如下图所示:

第三步:在res目录下新建一个drawable文件夹,定义两个资源文件一个是handle.xml另一个为listview_selected.xml,其中handle.xml代码已经在上面贴出,listview_selected.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true" android:drawable="@drawable/list_selector_background_pressed" />
  4. </selector>

第四步:修改main.xml布局文件,这里我用到了SliddingDrawer控件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <SlidingDrawer
  8. android:id="@+id/slidingdrawer"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:orientation="horizontal"
  12. android:handle="@+id/handle"
  13. android:content="@+id/content">
  14. <Button
  15. android:id="@+id/handle"
  16. android:layout_width="wrap_content"
  17. android:layout_height="fill_parent"
  18. android:background="@drawable/handle"
  19. />
  20. <ListView
  21. android:id="@+id/content"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. />
  25. </SlidingDrawer>
  26. </LinearLayout>

我们这里用到了ListView控件,而我们ListView控件显示的内容我事先在layout目录下定义两个TextView,命名为listview_layout.xml,代码如下(这里有三层哦!):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#ffffff"
  7. >
  8. <LinearLayout
  9. android:orientation="vertical"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent"
  12. android:background="@drawable/listview_selected"
  13. android:padding="6px"
  14. >
  15. <TextView
  16. android:id="@+id/bookname"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:textSize="20px"
  20. android:textColor="#000000"
  21. />
  22. <TextView
  23. android:id="@+id/author"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:textSize="16px"
  27. android:textColor="#000000"
  28. />
  29. </LinearLayout>
  30. </LinearLayout>

第五步:修改主核心程序UIDemo.java,代码如下:

  1. package com.tutor.uidemo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ListView;
  9. import android.widget.TextView;
  10. public class UIDemo extends Activity {
  11. private ListView mListView;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. setupViews();
  17. }
  18. private void setupViews(){
  19. mListView = (ListView)findViewById(R.id.content);
  20. mListView.setAdapter(new ListViewAdapter());
  21. }
  22. private class ListViewAdapter extends BaseAdapter{
  23. //这里返回10行,ListView有多少行取决于getCount()方法
  24. public int getCount() {
  25. return 10;
  26. }
  27. public Object getItem(int arg0) {
  28. return null;
  29. }
  30. public long getItemId(int arg0) {
  31. return 0;
  32. }
  33. public View getView(int position, View v, ViewGroup parent) {
  34. final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
  35. if(v == null){
  36. v = inflater.inflate(R.layout.listview_layout, null);
  37. }
  38. TextView mBookName = (TextView)v.findViewById(R.id.bookname);
  39. TextView mBookAuthor = (TextView)v.findViewById(R.id.author);
  40. mBookName.setText("Android傻瓜教程" + position);
  41. mBookAuthor.setText("Frankiewei" + position);
  42. return v;
  43. }
  44. }
  45. }

第六步:运行上述工程,查看效果:

运行效果1:

点击按钮效果2:

ListView正常效果3:

ListView点击效果4:

Android中UI设计的一些技巧!!!的更多相关文章

  1. Android 高级UI设计笔记07:RecyclerView 的详解

    1. 使用RecyclerView       在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...

  2. Android应用UI设计流程

    Android应用UI设计流程 设计原理 1.在移动设计中,使用环境是最关键的因素.原型设计方法必须考虑尺寸因素 2.用户测试必须涵盖运动.声音和多点触控等方面: 进行移动设计和测试时,请将你知道的有 ...

  3. Android中的对话框AlertDialog使用技巧合集-转载

    Android中的对话框AlertDialog使用技巧合集     文章来自:http://blog.csdn.net/blue6626/article/details/6641105   今天我用自 ...

  4. Android中UI线程与后台线程交互设计的5种方法

    我想关于这个话题已经有很多前辈讨论过了.今天算是一次学习总结吧. 在android的设计思想中,为了确保用户顺滑的操作体验.一 些耗时的任务不能够在UI线程中运行,像访问网络就属于这类任务.因此我们必 ...

  5. Android中UI线程与后台线程交互设计的6种方法

    在android的设计思想中,为了确保用户顺滑的操作体验.一些耗时的任务不能够在UI线程中运行,像访问网络就属于这类任务.因此我们必须要重新开启 一个后台线程运行这些任务.然而,往往这些任务最终又会直 ...

  6. 【Android开发经验】Android举UI设计经验

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 1.Android眼下的主流设备分辨率为480×800.720×1280.1080×1920,单位是像素.在 ...

  7. Android 高级UI设计笔记09:Android如何实现无限滚动列表

    ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ...

  8. Android 高级UI设计笔记08:Android开发者常用的7款Android UI组件(转载)

    Android开发是目前最热门的移动开发技术之一,随着开发者的不断努力和Android社区的进步,Android开发技术已经日趋成熟,当然,在Android开源社区中也涌现了很多不错的开源UI项目,它 ...

  9. Android 高级UI设计笔记09:Android实现无限滚动列表

    1. 无限滚动列表应用场景: ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们 ...

随机推荐

  1. 关于django过滤器的使用

    最近项目中要做分类筛选,其实已经做了这个功能,但是有一个字段是MultiSelectField类型,包含多个值,用户提交的数据是单个值,无法查询出结果, 所以用到了自定义过滤 原代码 class In ...

  2. Oracle学习笔记:wm_concat函数合并字段

    在Oracle中使用wm_concat(column)可以实现字段的分组合并,逗号分隔. 例如,现有表temp_cwh_test: -- 创建临时表 create table temp_cwh_tes ...

  3. Icon.png pngcrush caught libpng error:Read

    [问题处理]Icon.png pngcrush caught libpng error:Read Error 遇到问题 在项目Archive时,遇到 Icon.png pngcrush caught ...

  4. 30 最小的k个数

    输入n个整数,找出其最小的k个数,例如输入4,5,1,6,2,7,3,8,最小的4个数为1,2,3,4 解法一:快排思想,会改变原数组    O(n) 注意是vector<int>& ...

  5. LoadRunner 自带订票系统flights 功能空白、1080端口被占用的解决办法

    LoadRunner 自带订票系统flights 功能空白.1080端口被占用的解决办法 安装LoadRunner8.1后运行Mercury Web Tours Application,点击fligh ...

  6. Mysql Window 下安装

    http://blog.csdn.net/u013235478/article/details/50623693

  7. win10 远程桌面远程电脑时每次要输入密码及身份验证错误,要求的函数不受支持问题解决

    解决以前每次远程时能能记住密码,更新系统补丁后现在每次登录要输入密码了及远程时提示身份验证错误,要求的函数不受支持问题 解决方法一.卸载更新安装的新补丁,远程桌面正常,能记住密码 解决方法二.修改注册 ...

  8. Java设计模式 - 持续更新

    注意,此博客来源于我的 OneNote 笔记本 因此属于图片形式进行展示,这意味着你可以: 不经过我的同意进行保存 不经过我的同意进行发布 我仍然希望搬运时留一个网址指明来处:我的博客园 多谢!以下是 ...

  9. Counter Strike HDU 2443 逆序对数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2443 这个题目尝试了很多种方法都过不去,上网查了一下网友们的的思路,竟然和逆序对数有关系!! 题目大意: ...

  10. django使用admin

    1.在应用下的admin.py添加 #!/usr/bin/python # coding:utf-8 from django.contrib import admin from .models imp ...