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与上类似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值