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

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


Android UI元素使用初步

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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent 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(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent 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:


Android UI元素使用初步

<?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:


Android UI元素使用初步

<?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:


Android UI元素使用初步

<?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:


Android UI元素使用初步

public class activityRelaLineLayout extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);LinearLayout layoutMain = new LinearLayout(this);// 构建一个LayoutlayoutMain.setOrientation(LinearLayout.HORIZONTAL);// 设置LinearLayout中元素布局方向setContentView(layoutMain);LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 得到LayoutInflater对象,该对象可以对xml的布局文件进行解析,并生成一个ViewRelativeLayout 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与上类似。

更多相关文章

  1. Android中的界面布局之帧布局,相对布局
  2. Android 五大布局FrameLayout,LinearLayout ,AbsoluteLayout...
  3. Android的CheckBox控件的点击效果布局文件
  4. Android开发UI布局必备基础知识
  5. Android基础入门教程——2.2.1 LinearLayout(线性布局)
  6. Android-线性布局的经典案例1-计算器
  7. android 主题元素映射方式
  8. Android简易计算器——LinearLayout布局

随机推荐

  1. Android(安卓)zip文件压缩解压缩
  2. Android(安卓)在Activity中获取控件尺寸
  3. android:软件的安装和卸载源代码
  4. android读取ini文件
  5. android点滴3
  6. android用异步操作AsyncTask编写文件查看
  7. android 教程
  8. Android记事本的开发
  9. Android(安卓)安装步骤
  10. 可循环显示图像的Android(安卓)Gallery组