手机程序常常会在下面有一排并列的按钮,就像我现在做的这个程序:
看到下面一排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/