`
stephen830
  • 浏览: 2978097 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

android中的UI控制(一)

 
阅读更多

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布局或混合方式。

 

 

 


  • 大小: 10.8 KB
  • 大小: 9.2 KB
  • 大小: 9.6 KB
分享到:
评论

相关推荐

    Android新手UI集合全

    在实际开发中,Android Studio提供了强大的设计工具,如Layout Editor,可以可视化地设计和预览界面,同时支持XML代码编写,方便开发者进行精确控制。此外,Android Studio还提供了Vector Asset Studio,可以将SVG...

    原生android SystemUI源码

    在Android 4.0(Ice Cream Sandwich,简称ICS)版本中,SystemUI组件负责了状态栏、通知中心、快速设置面板等用户界面元素的显示与交互。 SystemUI主要包含以下几个关键组件: 1. **StatusBar**: 状态栏是Android...

    Android 智能UI锁屏

    在Android系统中,智能UI锁屏是指一种个性化且功能丰富的屏幕锁定界面,它不仅提供基本的解锁功能,还集成了各种实用工具和个性化设置,以提升用户体验。这种锁屏技术通常涉及用户界面设计、交互逻辑、安全性以及...

    androidUI设计器

    Android UI 设计器是Android开发中的一个重要工具,它允许开发者直观地设计应用程序的用户界面,无需手动编写XML布局代码。这个工具极大地提升了开发效率,使得非程序员也能参与到UI设计中来,实现快速预览和调整...

    Android所有UI控件

    以上只是Android UI控件的一小部分,实际开发中还有更多如DatePicker、TimePicker、ToggleButton等控件,以及自定义View的实现。通过熟练掌握这些控件及其属性,开发者可以创建出丰富多样的用户界面,提升用户体验。...

    Android things简单的UI

    在sample-simpleui项目中,很可能包含了一个简单的示例,演示如何在Android Things设备上创建并展示一个UI。项目可能包含了XML布局文件、MainActivity类以及必要的设备驱动代码。通过运行此项目,你可以观察到如何将...

    android 小米UI 自定义音量

    在Android中,音量控制通常使用滑动条或者进度条来表示音量大小,因此CircleProgressView可能是小米UI自定义音量控件的核心组件,它可能是一个圆形的进度条,提供更美观、直观的音量调节体验。 要移植小米UI的...

    android UI 各种小例子

    在Android开发中,UI设计是至关重要的一环,它关乎到应用程序的用户体验和视觉吸引力。本压缩包中的"android UI 各种小例子"为初学者提供了丰富的实践资源,旨在帮助开发者掌握基本的Android用户界面设计技巧。每个...

    安卓Android源码——ui开发类库示例源码.zip

    6. **DialogFragment**:对话框在Android应用中广泛应用,DialogFragment是Android官方推荐的方式,它继承自Fragment,可以方便地管理和控制对话框的显示与消失。 7. **动画(Animation)**:Android支持属性动画...

    移植Android的UI组件到Web端以Android的方式来制作高性能优体验的WebApp

    2. **自定义组件系统**:AndroidUI4Web可能有一个自定义的组件库,包含各种Android原生UI元素的Web实现,如按钮、列表视图、滑动选择器等,这些组件可以方便地通过JavaScript进行控制和定制。 3. **手势识别**:...

    android验证码界面ui实现

    在Android应用开发中,验证码界面UI的实现是一个常见的功能,主要用于身份验证或安全验证环节。本文将详细讲解如何在Android中实现一个具有60秒倒计时功能的验证码界面,以及如何通过PopupWindow来弹出这个界面。 ...

    android 仿锤子UI布局

    在Android中,布局(Layout)是组织和控制View组件的基础。常见的布局有LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)和GridLayout(网格布局)。锤子UI的格子布局通常结合了...

    android项目整体UI框架

    其次,实用Fragment切换是Android UI设计中的另一个关键元素。Fragment是Android系统提供的一种组件,它可以承载一部分用户界面并可以在Activity中动态添加、移除或替换。在大型项目中,通常会用多个Fragment来构建...

    android各种UI特效 工作中积累

    在Android开发中,UI特效是提升用户体验的关键因素之一。它涉及到界面的动态效果、过渡动画、自定义控件以及交互设计等多个方面。本资源“工作中积累UI特效”显然是一个开发者在实际工作过程中积累的Android UI设计...

    android ui旋转 控制系统旋转

    支持全志平台的ui旋转,里面有全志平台的系统签名,通过反射 IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); wm.freezeRotation(value); 实现ui旋转,其他平台需要系统签名后使用!切记必须要...

    Android UI 基础教程

    在Android开发领域,UI设计是至关重要的一环,它直接影响到应用程序的用户体验和视觉吸引力。"Android UI 基础教程"是一份专为初学者准备的指南,旨在帮助开发者掌握Android用户界面的设计与实现。这份教程虽然以...

    android 7.0 SystemUI

    在Android系统架构中,SystemUI位于应用层与系统服务层之间,它提供了通知中心、状态栏、快速设置面板、锁屏界面以及各种系统级别的交互控件。深入理解SystemUI的源代码对于开发者优化系统UI性能、自定义系统行为或...

    android-ui-master

    在Android开发中,UI设计和交互是至关重要的部分,尤其是对于用户操作反馈的动画效果。"android-ui-master"这个项目显然关注的是Android平台上的界面元素动画,特别是返回按钮的旋转和按钮转动效果。这些动画可以...

    Android UI统一框架

    在描述中提到的参考博文《Android UI统一框架》中,作者提出了一个具体的实现方案。该框架可能包含以下几个关键部分: 1. **组件库**:预定义一组常用的视图组件,如按钮、输入框、列表等,它们具有统一的视觉样式...

    android UI定制的一些资料

    在Android平台上,UI(用户界面)定制是一项关键的技术任务,它允许开发者根据需求和品牌风格创建独特的用户体验。这里,我们探讨的是"android UI定制的一些资料",这些资料可能包括设计原则、布局技巧、自定义视图...

Global site tag (gtag.js) - Google Analytics