`
sunnychuh
  • 浏览: 22223 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Android UI元素使用初步

阅读更多



 Android中视图组件(View)和布局组件(Layout)的使用非常重要,本例演示了其中几种重要的使用方法:

主界面有四个Button构成,每个分别触发不同的Activity。



 

 

public class activity01 extends Activity {
	Button button1;
	Button button2;
	Button button3;
	Button button4;
	OnClickListener listener1=null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listener1=new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity01.this,activityFrameLayout.class);
				setTitle("FrameLayout");
				startActivity(intent);
			}
		};
		button1=(Button) findViewById(R.id.button1);
		button1.setOnClickListener(listener1);
        button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity01.this,activityRelativeLayout.class);
				setTitle("RelativeLayout");
				startActivity(intent);
			}
        });
//button3与button4省略
           }
}

对于Button的监听事件的绑定通常有两种方式,如上所示,一种是定义监听类,再对Button进行绑定;第二种为使用匿名内部类。

接下来通过Intent进行Activity之间的跳转,注意目标Activity要在AndroidManifest.xml中进行声明:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.sunny" android:versionCode="1" android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".activity01" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name=".activityFrameLayout">
		</activity>
		<activity android:name=".activityRelativeLayout">
		</activity>
		<activity android:name=".activityRelaLineLayout">
		</activity>
		<activity android:name=".activityTableLayout">
		</activity>
	</application>
	<uses-sdk android:minSdkVersion="9" />

</manifest> 

  几种不同的布局视图:

FrameLayout:



 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/left"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content">
	<ImageView android:id="@+id/photo" android:src="@drawable/bg"
		android:layout_width="wrap_content" android:layout_height="wrap_content" />
	<ImageView android:id="@+id/photo2" android:src="@drawable/tp"
		android:layout_width="wrap_content" android:layout_height="wrap_content" />
</FrameLayout>

 RelativeLayout:



 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:background="@drawable/blue" android:padding="10dip">
	<TextView android:id="@+id/label" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="请输入姓名:" />
	<EditText android:id="@+id/entry" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:background="@android:drawable/editbox_background"
		android:layout_below="@id/label" />
	<Button android:id="@+id/cancel" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_below="@id/entry"
		android:layout_alignParentRight="true" android:layout_marginLeft="10dip"
		android:text="取消" />
	<Button android:id="@+id/ok" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_toLeftOf="@id/cancel"
		android:layout_alignTop="@id/cancel" android:text="确定" />
</RelativeLayout>
	

 注意,以上代码中android:background="@drawable/blue" 需在string.xml中添加

<drawable name="blue">#770000ff</drawable>

 

TableLayout:



 

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:stretchColumns="1">
	<TableRow>
		<TextView android:text="用户名" android:textStyle="bold"
			android:gravity="right" android:padding="3dip" />
		<EditText android:id="@+id/username" android:padding="3dip"
			android:scrollHorizontally="true" />
	</TableRow>
	<TableRow>
		<TextView android:text="登陆密码" android:textStyle="bold"
			android:gravity="right" android:padding="3dip" />
		<EditText android:id="@+id/password" android:padding="3dip"
			android:password="true" android:scrollHorizontally="true" />
	</TableRow>
	<TableRow android:gravity="right">
		<Button android:id="@+id/cancel1" android:text="取消" />
		<Button android:id="@+id/ok1" android:text="登陆" />
	</TableRow>
</TableLayout>

 

在程序中控制Layout的方法,在linearLayout中嵌套两个RelationLayout:



 

public class activityRelaLineLayout extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LinearLayout layoutMain = new LinearLayout(this);// 构建一个Layout
		layoutMain.setOrientation(LinearLayout.HORIZONTAL);// 设置LinearLayout中元素布局方向
		setContentView(layoutMain);
		LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 得到LayoutInflater对象,该对象可以对xml的布局文件进行解析,并生成一个View
		RelativeLayout layoutleft = (RelativeLayout) inflate.inflate(
				R.layout.left, null);//调用inflate方法将left.xml进行解析,并生成一个RelativeLayout布局
		RelativeLayout layoutright = (RelativeLayout) inflate.inflate(
				R.layout.right, null);
		RelativeLayout.LayoutParams relParm = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);//生成参数
		layoutMain.addView(layoutleft, 100, 100);
		layoutMain.addView(layoutright, relParm);
	}
}

 

其中R.layout.left:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/left"
	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="fill_parent"
		android:layout_height="50px" android:text="第一组第一项" />
	<TextView android:id="@+id/view2" android:layout_width="fill_parent"
		android:layout_height="50px" android:text="第一组第二项"
		android:layout_below="@id/view1" />
</RelativeLayout>

 

R.layout.left与上类似。

  • 大小: 19.7 KB
  • 大小: 33.3 KB
  • 大小: 10.5 KB
  • 大小: 8.9 KB
  • 大小: 12.5 KB
分享到:
评论

相关推荐

    AndroidUI界面开发规范

    ### AndroidUI界面开发规范 #### 一、引言 随着移动设备的普及和发展,用户界面(UI)设计在软件开发中的重要性日益凸显。一个良好的UI不仅能够提升用户体验,还能够帮助开发者更好地传达产品理念。本篇文章将从...

    《Android UI基础教程》英文版

    - **定义**:Android UI(用户界面)是指在Android设备上为用户提供视觉、听觉和触觉等交互体验的设计和实现。 - **重要性**:良好的用户界面能够提高用户体验,使得应用程序更加直观易用,从而吸引更多用户并提升...

    android UI 设计工具 droiddraw

    1. **图形化界面编辑**:DroidDraw提供了直观的图形用户界面,使得开发者可以像拼图一样将各种UI元素(如按钮、文本框、图片视图等)放置在屏幕上,调整它们的大小和位置,从而构建出基本的Android界面。 2. **预览...

    Android屏幕自适应计算工具 UI设计尺寸换算

    为了实现界面在不同设备上自适应显示,设计师通常会在设计工具如Adobe Photoshop(PS)中完成初步的UI设计,然后将设计图中的尺寸转换为Android开发所需的像素(px)和密度无关像素(dp)单位。本文将详细探讨...

    Android嵌入式开发资料

    实验报告中的"实验一"可能涉及UI控件的初步使用,比如创建基本的布局和交互。"实验二"可能是关于Activity的生命周期和Intent的实践。"实验三"可能涵盖数据存储,如SQLite数据库操作。"实验四"可能与网络通信有关,如...

    UI项目UI项目UI项目UI项目UI项目UI项目

    UI项目中的标签“UI项目UI项目UI项目UI项目UI项目UI项目”可能是对项目关键元素的概括,这些元素可能包括: 1. **需求分析**:项目开始时,团队需明确产品目标和用户需求,理解用户的行为和期望,为设计提供方向。 ...

    Android大学生在线(包含UI效果图、原型图、接口文档、毕业设计)

    UI(用户界面)效果图是设计师呈现应用视觉风格和布局的成果,它展示了应用的颜色搭配、图标设计、布局结构等元素。在"大学生二期UI"文件中,我们可以看到不同页面如登录注册、课程浏览、论坛交流等模块的设计细节...

    安卓智能家居,android app源码,初步是实现开关、led灯亮度,电机pwm等,定时

    了解以上基础知识后,开发者需要具备Android应用开发经验,熟悉Java或Kotlin编程语言,理解Android SDK和Android Studio的使用。对于硬件部分,需要一定的嵌入式系统知识,如GPIO(通用输入输出)操作、PWM控制等。...

    Android开发 - Activity 初步

    综上所述,"Android开发 - Activity 初步"这篇博客可能涵盖了Activity的基本概念、生命周期、启动模式、Intent使用、栈管理以及源码分析等多个方面,是初学者了解和深入理解Activity的重要资源。对于Android开发者来...

    Android应用开发完全自学手册

    此章节主要介绍Android UI设计中的基本控件,如TextView、EditText、Button、ImageView等,以及如何在布局文件中使用XML来定义这些控件。同时,会教授如何在代码中动态添加和操作控件,以及使用布局管理器如...

    手机UI例教程

    Layouts(布局)如LinearLayout、RelativeLayout或ConstraintLayout,用于组织和定位UI元素;Views(视图)如Button、TextView、ImageView等,构成用户界面的基础组件。 手机UI设计通常遵循以下原则: 1. 清晰性:...

    移动设备UI草图模版

    移动设备UI草图模版是设计者们在创建应用程序或网站界面时不可或缺的工具,尤其在初步构思和原型设计阶段。这些模版通常包含了不同操作系统和设备的屏幕轮廓,如Android、iOS、Windows Phone(WP)和BlackBerry,...

    用户定义与UI设计--摘自《Its Android Time》第3章

    《It's Android Time -- Google Android:创赢路线与开发实战》一书中的第三章专门探讨了用户定义与UI设计的方法和技术,这对于Android开发者来说尤其重要。 #### 二、UI设计思维 UI设计不仅仅是关于外观的设计,...

    AndroidDriver-master_layers2x3_简单android源码_androidapp源码_android_

    【AndroidDriver-master_layers2x3_简单android源码_android...总之,这个项目为初学者提供了一个基础的Android应用实例,通过研究和实践,可以帮助他们建立起对Android开发的初步认识,并逐步深入到更复杂的应用场景。

    Android应用源码之猜拳游戏.zip

    8. **Android布局和UI设计**:猜拳游戏的界面设计会涉及到Android的布局系统,如LinearLayout、RelativeLayout或ConstraintLayout等,以及XML文件来定义界面元素。 9. **事件监听**:为了响应用户的操作,比如点击...

    android 若水新闻代码(部分)

    - 使用LinearLayout、RelativeLayout或ConstraintLayout等布局管理器组织UI元素,确保界面在不同设备上适配良好。 3. **数据获取与解析**: - 新闻应用通常需要从网络获取数据,这可能涉及到了HTTP请求库,如...

    android fragment超简单使用demo

    在Android应用开发中,Fragment是Android SDK提供的一种组件,它允许开发者...通过学习这个示例,开发者可以对Fragment有一个初步的认识,并能够将Fragment集成到自己的Android应用中,实现更加复杂和动态的用户界面。

    动手学Android之四——布局初步(一)例子程序

    在Android开发中,布局是构建用户界面的基础,它定义了屏幕上元素的排列方式和相互关系。本篇文章将深入探讨Android中的布局初步知识,通过实际的例子程序"firstlayout"来帮助理解这一概念。 首先,Android布局是...

    Android 开发实战经典

    在"0204_Android中的基本控件(上)"和"0207_Android中的基本控件(下)"中,详细阐述了Android UI设计的基础元素,如TextView、EditText、Button、ImageView等,以及如何在布局文件中声明和使用它们,还有事件监听...

Global site tag (gtag.js) - Google Analytics