安卓开发入门实战第三天——一点的笔记
。多多百度多多写代码安卓基本组件第三天 界面UI元素介绍1视图组件(View)View是最基本的UI类,基本上所有高级UI组件都是继承View类实现的。如TextView,Button,List,EditText,RadioButton,Checkbox等等都是View类一个视图(View)在屏幕上占据了一块矩形区域,它负责渲染着块矩形区域(如将这块区域编程蓝色或其他颜色),也可以处理这块矩形区域发生的事件(如用户单击这块区域),并且可以设置这块区域是否可见,是否可以获取焦点等。2视图容器组件(Viewgroup)一个Viewgroup对象是一个Android.View.Viewgroup的实力,作用是View的容器,负责对添加进Viewgroup的这些View进行布局。因为Viewgroup继承与View.Viewgroup类,是一个抽象类,也是其他容器类的基类,股一个Viewgroup可以加入到另一个Viewgroup里面3布局组件(Layout)Android布局分别是LinearLayout ( 线性布局)、 TableLayout (表格布局) 、 RelativeLayout (相对布局)、FrameLayout( 帧布局 ) 、AbsoluteLayout(绝对布局) 。Viewgroup的实现类比较挫,这里先介绍最常用的两个实现类,LinearLayout线性布局,RelativeLayout相对布局
4布局参数(LayoutParams)这个还是不懂二,应用界面布局写程序最重要的是常识,不要怕出错,多尝试,不调试,进步才会快通过这个例子学习内容:使用FrameLayout进行布局,单独使用LinearLayout,RelativeLayout布局,结合使用LinearLayout,RelativeLayout布局,s使用TableLayout进行布局第一步:打开程序第二步:package com.eoeandroid.layout;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class ActivityMain extends Activity { OnClickListener listener0 = null; OnClickListener listener1 = null; OnClickListener listener2 = null; OnClickListener listener3 = null; Button button0; Button button1; Button button2; Button button3; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listener0 = new OnClickListener() { public void onClick(View v) { Intent intent0 = new Intent(ActivityMain.this, ActivityFrameLayout.class); setTitle("FrameLayout"); startActivity(intent0); } }; listener1 = new OnClickListener() { public void onClick(View v) { Intent intent1 = new Intent(ActivityMain.this, ActivityRelativeLayout.class); startActivity(intent1); } }; listener2 = new OnClickListener() { public void onClick(View v) { setTitle("这是在ActivityLayout"); Intent intent2 = new Intent(ActivityMain.this, ActivityLayout.class); startActivity(intent2); } }; listener3 = new OnClickListener() { public void onClick(View v) { setTitle("TableLayout"); Intent intent3 = new Intent(ActivityMain.this, ActivityTableLayout.class); startActivity(intent3); } }; setContentView(R.layout.main); button0 = (Button) findViewById(R.id.button0); button0.setOnClickListener(listener0); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(listener1); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(listener2); button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(listener3); } }代码解释:. setContentView(R.layout.main);负责当前的Activity与main.xml文件相关联. 然后得到4个Button引用,并且给Button设置单击监听器。Button,一般需要设置监听器来处理点击事件。每一个监听器都是跳转到一个新的Activitymain.xml<?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"> <Button android:id="@+id/button0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="非洲草原地图:FrameLayout的使用" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="个性化表单:RelativeLayout的使用" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="LinearLayout和RelativeLayout互助使用" /> <Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="整齐的表单:TableLayout的使用" /></LinearLayout>1 LinearLayout采用线性布局,就是从上到下或者从左到右orientation=”vertical”;方向是垂直的,orientation=”horizontal”:方向是水平的,这两个属性定义了标签是垂直还是水平摆放layout_width=”fill_parent”layout_height=”fill_parent”这两个属性是控制控件的大小,即宽度和高度,fill_parent把父控件填满,layout_height=“wrap_content”高度正好填充满它的内容,就是内容多高控件多高布局的时候常用的一些属性总结:**** Hidden Message *****第三步:点击第一个Button,出现一个FrameLayout布局的地图界面帧布局也可以说是一种嵌套布局,,所有的元素都被放置在FrameLayout区域最左上的区域,而且无法为这些元素制定一个确切的位置,如果一个FrameLayout有多个子元素,那么厚填充的子元素会重叠显示在前一个元素上package com.eoeandroid.layout;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class ActivityFrameLayout extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);; setContentView(R.layout.activity_frame_layout); 取消按钮和容器的右边 }}Main.xml<?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="fill_parent">
<ImageView android:id="@+id/photo" android:src="@drawable/bg" android:layout_width="wrap_content" android:layout_height="wrap_content"/></FrameLayout>id:定义组件id,通过id访问定义的元素第四步:RelativeLayout相对布局处于性能考虑,相对布局的精确位置的计算只会执行一次,这就是说,如果可视化组件b依赖于a,那么必须要让a出现在b的前面。<?xml version="1.0" encoding="utf-8"?><!-- Demonstrates using a relative layout to create a form --><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放置在上边id为label的TextView的下边 --> <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" /> <!--齐平,并且设置左边的边距为10dip --> <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"定义组件背景颜色,这里系统解析@drawable/blue的值为#770000ff这是颜色的RGB码。android:padding="10dip像素,描述区域大小的单位,padding表示填充,margin表示边距android:layout_below="@id/label" ,推荐相对布局时候用,适配性强,不同大小的屏幕都适用,当然有些时候按照像素精确布局也是必要的必须的,要学会适时使用**** Hidden Message *****相对布局中什么都不设置以上的属性,他会放到父控件左上角小知识:安卓中支持描述大小区域的类型如下:最常用的是px和dip第五步:left.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayoutandroid: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:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="50px" android:text="第一组第一项" /> <TextView android:id="@+id/view2" android:background="@drawable/yellow" android:layout_width="fill_parent" android:layout_height="50px" android:layout_below="@id/view1" android:text="第一组第二项" /></RelativeLayout>ActivityLayoutpackage com.eoeandroid.layout;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class ActivityLayout extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layoutMain = new LinearLayout(this);构建一个Layout layoutMain.setOrientation(LinearLayout.HORIZONTAL);设置加入到这个LinearLayout中元素的布局为垂直布局 setContentView(layoutMain);将构建的LayoutMain和此Activity进行关联 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 relParam = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);生成一个RelativeLayout使用的LayoutParams。 layoutMain.addView(layoutLeft, 100, 100); layoutMain.addView(layoutRight, relParam); }}所有的View和Layout都可以直接在java代码构造。在此Activity中,为了方便,我们自己构建一个Layout,语句为: LinearLayout layoutMain = new LinearLayout(this);..LayoutInflater 这个类用来将xml的布局文件变成一个View类,出于性能上的考虑,从xml的布局文件成熟一个View类这个过程是在应用的构建阶段完成的,而不是在应用运行阶段进行的,电池的使用过程是在Activity的oncreate方法里边,代码如下所示:LayoutInflater类需要通过getSystemService方法获得,而不能直接实例化。这个类负责将一个xml的布局文件实例化,从而在代码中可以应用在xml定义的组件第六步:TableLayout表格布局,元素以行和列的形式进行排列,表格的列数为每一行的最大列数,当然表格里边的单元格可以为空。<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:password="true" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow> <TableRow android:gravity="right"> <Button android:id="@+id/cancel" android:text="取消" /> <Button android:id="@+id/login" android:text="登录" /> </TableRow></TableLayout>代码解释:TableRow定义白哦个布局的一行,每一行可以加入组件
沙发。。最近忙做个小项目,所以笔记写的不是很完整,希望大家可以一起完善,只要在回复里面写出你的看法建议和对这部分的理解和补充就好,希望大家一起完善,回复是美德 谢谢 这个有点乱哦
黑夜 发表于 2012-7-10 22:52 static/image/common/back.gif
这个有点乱哦
呵呵,没办法,一是少了图说明,那样就清楚很多,最近忙个小项目找工作,笔记就不够系统性了,没有太多时间完善 ,以后会尽力弄的好点发上来 这个要好好看看 :lol:看看!看看!
页:
[1]