- 浏览: 2998065 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (893)
- android (110)
- iphone (198)
- java (79)
- JavaScript手册-目录 (9)
- JavaScript手册-Array (19)
- JavaScript手册-Boolean (5)
- JavaScript手册-Date (50)
- JavaScript手册-Math (30)
- JavaScript手册-Number (14)
- JavaScript手册-RegExp (7)
- JavaScript手册-String (38)
- JavaScript手册-全局函数 (8)
- JavaScript实用脚本 (7)
- Others (21)
- java-jpcap (7)
- java-thread (1)
- ibm文章 (3)
- classloader (2)
- java-filter (2)
- 运行环境 (33)
- java-正则 (2)
- oracle (1)
- linux-shell (26)
- wap (1)
- sqlite (3)
- wow (1)
- jvm (1)
- git (5)
- unity3d (29)
- iap (2)
- mysql (23)
- nginx (14)
- tomcat (9)
- apache (2)
- php (1)
- ubuntu (40)
- rsa (1)
- golang (21)
- appstore (5)
- sftp (2)
- log4j (2)
- netty (18)
- 测试工具 (6)
- memcache (5)
- 设计模式 (1)
- centos (8)
- google_iab (5)
- iOS专题 (4)
- mac (10)
- 安装配置帮助手册 (2)
- im4java_graphicsmagick (5)
- inotify-tools (1)
- erlang (6)
- 微信支付 (1)
- redis (8)
- RabbitMQ (5)
最新评论
-
heng123:
Netty视频教程https://www.douban.com ...
netty4.0.23 初学的demo -
maotou1988:
使用Netty进行Android与Server端通信实现文字发 ...
netty4.0.23 初学的demo -
码革裹尸:
非常感谢,正好用上
android 呼入电话的监听(来电监听) -
rigou:
提示的/222.177.4.242 无法链接到ip地址,是什 ...
通过 itms:services://? 在线安装ipa ,跨过app-store -
duwanbo:
GridView与数据绑定
android中的UI控制(一)
转载自 http://www.android777.com/index.php/tutorial/androids-ui-control-a.html
Java在UI方面的表现一直很不如意,由于界面不好看、使用不方便造成大量开发人员不愿去使用它。幸好在Android中我们将不再使用这些控 件。Android内建了一个内部的UI框架,并做了一些特殊的设计以让其在手机上能有良好的用户体验。在Android SDK中,跟JDK一样有Button、TextField、List、Grid等,但是这些控件都做了优化,提供了适合在手机上做的控制。
在Android SDK UI的核心控制中有android.view.View和android.view.ViewGroup是两个主要类。View表示是一个视图就是一般视 觉上的一个区域,ViewGroup也是一个View,它扩展View使其内部能存放其他的View所以可以理解为一个Container容器。 ViewGroup内部采用跟Swing一样的处理机制,内部采用一个layout manager来管理它的布局,让用户能采用内置的布局进行视图控制。
声明布局和视图的方式:
在Android中有三种方式可以用来创建UI界面。
1. 在xml布局文件中创建出UI视图的层次架构,android提供了很多属性用来使用xml对视图和布局进行管理,你可以通过这些属性定义视图大小,方向等。
2. 在代码中创建视图或布局。 动态的创建View或ViewGroup并设置它们的属性。
3.混合方式。 在xml中声明视图或布局。然后在代码中找出这些视图,然后修改里面的属性。
使用xml布局的优点是:它使用的MVC模式,将视图层和逻辑控制层分开。视图和布局控制文件没有混合在程序代码中,使整个项目结构清晰,可维护性高。
使用代码创建视图或布局的优点是:可以根据程序运行状态,动态的显示内容。
下面演示对应两种方式实现同一个视图:
效果图
(一)代码方式
package com.zhouzijing.android; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //创建一个主视图容器 LinearLayout parent = new LinearLayout(this); parent.setOrientation(LinearLayout.VERTICAL); parent.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); //创建放名字的容器 LinearLayout nameLinearLayout = new LinearLayout(this); nameLinearLayout.setOrientation(LinearLayout.HORIZONTAL); nameLinearLayout.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); TextView nameLabel = new TextView(this); nameLabel.setText("名字:"); TextView name = new TextView(this); name.setText("张三丰"); nameLinearLayout.addView(nameLabel); nameLinearLayout.addView(name); //创建放地址的容器 LinearLayout addressLinearLayout = new LinearLayout(this); addressLinearLayout.setOrientation(LinearLayout.HORIZONTAL); addressLinearLayout.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); TextView addressLabel = new TextView(this); addressLabel.setText("地址:"); TextView address = new TextView(this); address.setText("武当山"); addressLinearLayout.addView(addressLabel); addressLinearLayout.addView(address); //创建放国家的容器 LinearLayout countryLinearLayout = new LinearLayout(this); countryLinearLayout.setOrientation(LinearLayout.HORIZONTAL); countryLinearLayout.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); TextView countryLabel = new TextView(this); countryLabel.setText("国家:"); TextView country = new TextView(this); country.setText("中国"); countryLinearLayout.addView(countryLabel); countryLinearLayout.addView(country); //将名字、地址、国家放到主容器中 parent.addView(nameLinearLayout); parent.addView(addressLinearLayout); parent.addView(countryLinearLayout); //显示主容器 setContentView(parent); } }
(二)布局xml方式
下面我们创建一个布局xml也可以完成同样的效果:
main.xml 存放到res/layout目录下
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名字:" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="张三丰" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址:" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="武当山" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="国家:" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中国" /> </LinearLayout> </LinearLayout>
对应Activity代码:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
上面完成的效果同用代码创建视图的一样。主容器、名字容器、地址容器和国家容器都是LinearLayout,它有一个属性可以设置方向,在主容器中方向是纵向,而 其他三个则是横向,所以名字标签和名字值在同一行,地址标签和地址值在同一行, 国家标签和国家值也在同一行 。因为主容器是纵向,所以主容器里面的三个子节点名字容器、地址容器、 国家容器 将从上 到下排列所以分层三行。
备注:
xml文件中:
android:layout_width="..." android:layout_height="..."
每个View的android:layout_width、android:layout_height必须在xml中进行设置,否则程序将发生异常而终止。
android:layout_width、android:layout_height取值有2种数据:(1)fill_parent(2)wrap_content
(三) 混和使用动态创建和xml布局创建视图
新建布局文件 second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="原始文本" /> </LinearLayout>
这边我们定义了一个TextView,然后设置的它的大小和里面的内容。注意,这里我们给这个TextView赋了一个ID 值:android:id=”@+id/view1″。这边的”@+id”是值创建一个新的ID值,这时候android平台开发的aapt工具(Android Asset Packaging Tool)就会帮我 们生成一个字段值,让我们可以通过R.id.view1来查找出这个对象。如果是”@id”则表示这个是对ID值的引用,aapt工具不会帮我们生成一个 字段值来获取这个id。
Activity代码:
(代码1)
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); }
(代码2)
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); TextView tv = (TextView)findViewById(R.id.view1); tv.setText("新的TextView值"); }
代码2做的工作:先将布局文件second.xml设置到当前的布局中。然后根据id进行查找,查找出在xml布局中声明的TextView对象,然后修改里面的值。最终效果如下图(代码2效果)
执行效果如下:
|
|
代码1效果 | 代码2效果 |
运行效果是,原来的在xml布局中声明的内容被动态修改掉了。通过以上的代码对比可以发现其实使用xml布局文件的效果更好,因为避免写了一大堆界面代码 嵌套在应用程序逻辑中。当布局复杂时还有很多布局额外的属性要配置如:layout_padding , layout_margin等,如果把这些东西全部写在代码中,那代码将会变得很臃肿,很难维护,所以建议除非要创建的只有简单的几个视图对象否则最好用 xml布局或混合方式。
发表评论
-
Android Fragment 真正的完全解析(下)
2015-03-25 14:30 1122Android Fragment 真正 ... -
Android Fragment 真正的完全解析(上)
2015-03-25 10:14 912Android Fragment 真正的 ... -
Activity的生命周期方法
2015-03-25 09:54 580一:Activity的生命周期方法 Android ... -
Android在layout xml中使用include
2015-03-25 07:28 998在Android的layout样式定义中,可以使 ... -
一个支持圆角的快速ImageView:RoundedImageView
2015-03-07 09:50 11093一个支持圆角的快速ImageView:Rounded ... -
ImageView图片设置
2015-03-05 23:08 957显示任意图像,例如图标。ImageView类可以加载 ... -
ListActivity的onListItemClick不响应的解决办法
2015-03-04 17:57 1108ListActivity的onListItemClic ... -
ListView中有控件隐藏或显示造成的显示错误问题
2015-03-03 23:03 1853ListView中有控件隐藏或显示造成的显示错误问题 ... -
Android中Application类用法
2015-03-03 09:12 997Android中Application类用法 ... -
Android使用Application总结
2015-03-03 09:11 1196Android使用Application ... -
在android中运行java main方法
2015-02-27 10:38 998在android中运行java main方法 ... -
android BitmapFactory.Options
2015-02-19 21:01 1036public Bitmap inBi ... -
android 基础demo例子
2015-02-16 21:06 836demoDatas.add(new DemoBe ... -
Android大图片裁剪终极解决方案
2015-02-15 17:08 2165Android大图片裁剪终 ... -
Android_照相机Camera_调用系统照相机返回data为空
2015-02-15 14:17 1294Android_照相机Camera_调用系统照相机返回 ... -
Android: 打印Bundle内容
2015-02-15 14:11 2967Android: 打印Bundle内容 有的时 ... -
android线程、UI、AsyncTask
2015-02-13 18:06 1287本文是转摘的,看过了觉得很好!所以转了过来!如果你想要 ... -
Fragment中ListView的使用
2015-02-06 23:01 2127Fragment中ListView的使用 直接 ... -
在安卓开发时用酷派手机无法打印出应用内的日志的解决方法
2014-12-11 20:39 1200在安卓开发时用酷派手机无法打印出应用内的日志的解决方法 ... -
查看安卓APK文件证书的方法
2014-12-04 10:05 1011************************** ...
相关推荐
在实际开发中,Android Studio提供了强大的设计工具,如Layout Editor,可以可视化地设计和预览界面,同时支持XML代码编写,方便开发者进行精确控制。此外,Android Studio还提供了Vector Asset Studio,可以将SVG...
在Android 4.0(Ice Cream Sandwich,简称ICS)版本中,SystemUI组件负责了状态栏、通知中心、快速设置面板等用户界面元素的显示与交互。 SystemUI主要包含以下几个关键组件: 1. **StatusBar**: 状态栏是Android...
在Android系统中,SystemUI是用户界面的核心组成部分,它负责管理状态栏、通知中心、快速设置等关键功能。本文将深入探讨Android 8.1版本的SystemUI源码,介绍其结构、工作原理以及如何利用提供的gradle配置进行开发...
Android UI 设计器是Android开发中的一个重要工具,它允许开发者直观地设计应用程序的用户界面,无需手动编写XML布局代码。这个工具极大地提升了开发效率,使得非程序员也能参与到UI设计中来,实现快速预览和调整...
以上只是Android UI控件的一小部分,实际开发中还有更多如DatePicker、TimePicker、ToggleButton等控件,以及自定义View的实现。通过熟练掌握这些控件及其属性,开发者可以创建出丰富多样的用户界面,提升用户体验。...
在sample-simpleui项目中,很可能包含了一个简单的示例,演示如何在Android Things设备上创建并展示一个UI。项目可能包含了XML布局文件、MainActivity类以及必要的设备驱动代码。通过运行此项目,你可以观察到如何将...
在Android中,音量控制通常使用滑动条或者进度条来表示音量大小,因此CircleProgressView可能是小米UI自定义音量控件的核心组件,它可能是一个圆形的进度条,提供更美观、直观的音量调节体验。 要移植小米UI的...
在Android开发中,UI设计是至关重要的一环,它关乎到应用程序的用户体验和视觉吸引力。本压缩包中的"android UI 各种小例子"为初学者提供了丰富的实践资源,旨在帮助开发者掌握基本的Android用户界面设计技巧。每个...
6. **DialogFragment**:对话框在Android应用中广泛应用,DialogFragment是Android官方推荐的方式,它继承自Fragment,可以方便地管理和控制对话框的显示与消失。 7. **动画(Animation)**:Android支持属性动画...
2. **自定义组件系统**:AndroidUI4Web可能有一个自定义的组件库,包含各种Android原生UI元素的Web实现,如按钮、列表视图、滑动选择器等,这些组件可以方便地通过JavaScript进行控制和定制。 3. **手势识别**:...
在Android应用开发中,验证码界面UI的实现是一个常见的功能,主要用于身份验证或安全验证环节。本文将详细讲解如何在Android中实现一个具有60秒倒计时功能的验证码界面,以及如何通过PopupWindow来弹出这个界面。 ...
在Android中,布局(Layout)是组织和控制View组件的基础。常见的布局有LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)和GridLayout(网格布局)。锤子UI的格子布局通常结合了...
其次,实用Fragment切换是Android UI设计中的另一个关键元素。Fragment是Android系统提供的一种组件,它可以承载一部分用户界面并可以在Activity中动态添加、移除或替换。在大型项目中,通常会用多个Fragment来构建...
在Android开发中,UI特效是提升用户体验的关键因素之一。它涉及到界面的动态效果、过渡动画、自定义控件以及交互设计等多个方面。本资源“工作中积累UI特效”显然是一个开发者在实际工作过程中积累的Android UI设计...
#### 一、Android UI基础知识 **1.1 Android UI简介** Android UI(用户界面)是Android应用程序与用户交互的主要方式。一个良好的UI设计能够极大地提升用户体验,使得用户更容易理解和操作应用程序。Android UI...
支持全志平台的ui旋转,里面有全志平台的系统签名,通过反射 IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); wm.freezeRotation(value); 实现ui旋转,其他平台需要系统签名后使用!切记必须要...
在Android开发领域,UI设计是至关重要的一环,它直接影响到应用程序的用户体验和视觉吸引力。"Android UI 基础教程"是一份专为初学者准备的指南,旨在帮助开发者掌握Android用户界面的设计与实现。这份教程虽然以...
在Android系统架构中,SystemUI位于应用层与系统服务层之间,它提供了通知中心、状态栏、快速设置面板、锁屏界面以及各种系统级别的交互控件。深入理解SystemUI的源代码对于开发者优化系统UI性能、自定义系统行为或...