Android(OPhone)中,如何将按钮排成一排?

手机程序常常会在下面有一排并列的按钮,就像我现在做的这个程序:

20091120-1

看到下面一排Home之类的按钮么?在Android中弄起来,可不是这么容易的。在android中,有两种方式可以创建按钮,一种是采用代码方式,另一种是采用layout/main.xml,从我的角度来看,我觉得除非必要,没必要自己写代码创建按钮,用mail.xml清晰,有利于代码与布局分开。什么时候需要用代码创建?我觉得是动态的时候,基本上这种情况比较少吧。

ok,先看来代码,然后我简单地解释一下吧:

<?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="fill_parent">
	<WebView android:id="@+id/webview" android:layout_width="fill_parent"
		android:layout_height="370px" />
	<Button android:text="Home" android:id="@+id/btnHome"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_below="@+id/webview" android:layout_alignParentLeft="true"
		android:layout_marginLeft="1dip" />
	<Button android:text="Flights" android:id="@+id/btnFlights"
		android:layout_toRightOf="@+id/btnHome" android:layout_alignTop="@+id/btnHome"
		android:layout_width="wrap_content" android:layout_centerInParent="true"
		android:layout_height="wrap_content" android:gravity="left"
		android:layout_weight="1" />
			<Button android:text="Hotel" android:id="@+id/btnHotel"
		android:layout_toRightOf="@+id/btnFlights" android:layout_alignTop="@+id/btnFlights"
		android:layout_width="wrap_content" android:layout_centerInParent="true"
		android:layout_height="wrap_content" android:gravity="left"
		android:layout_weight="1" />
			<Button android:text="Member" android:id="@+id/btnMember"
		android:layout_toRightOf="@+id/btnHotel" android:layout_alignTop="@+id/btnHotel"
		android:layout_width="wrap_content" android:layout_centerInParent="true"
		android:layout_height="wrap_content" android:gravity="left"
		android:layout_weight="1" />
</RelativeLayout>

  好,我们看几个重要的属性,RelativeLayout表示相对定位,在Button中,两个属性我们要注意,android:layout_below="@+id/webview"表示当前按钮在@+id/webview下面,而 android:layout_alignParentLeft="true"表示靠左。然后接下来的按钮,android:layout_toRightOf和android:layout_alignTop以@+id/btnHome为标致,在@+id/btnHome的右边。

  

注意:本文为我的独立博客镜像博客,自发表不再更新,原文可能随时被更新,敬请访问原文。同时,请大家不要在此评论,如果有什么看法,请点击这里:http://iove.net/1705/

本文来自http://iove.net,欢迎转载,转载敬请保留相关链接,否则视为侵权,原文链接:http://iove.net/1705/

转载于:https://www.cnblogs.com/conis/archive/2009/11/20/1720456.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值