.Net程序猿玩转Android开发---(7)相对布局RelativeLayout
能夠設(shè)置控件與父控件的位置,控件與控件之間的位置。
?????????????? 1. 控件與父容器位置屬性
?????????????????????????????? ? android:layout_alignParentLeft="true"?? 子控件相對于父容器靠左邊
???????????????????????????????? android:layout_alignParentTop="true"??? 子控件相對于父容器靠 上邊
?????????????????????????????????android:layout_marginTop="50dp"????????? 子控件與父容器上邊距距離
??????????????????????????????? android:layout_marginBottom="50dp"??? 子控件與父容器下邊距距離
??????????????????????????????? android:layout_marginRight="50dp"????? 子控件與父容器右邊距距離
???????????????????????????????? android:layout_marginLeft="50dp"????? 子控件與父容器左邊距距離
?????????????????????????????????? ?? android:layout_centerInParent="true"//子控件在父容器中居中顯示
???????????????????????????????? ? ?android:layout_centerHorizontal="true"?//子控件在父容器中水平居中
???????????????????????????????????? android:layout_centerVertical="true"? 子控件在父容器中垂直居中
?????????????????????????? 以下的演示樣例展示下相對于父容器的布局
????????????????????????????
???????????????????????
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="match_parent"android:text="相對父easy布局" android:background="#97FFFF"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginTop="50dp"android:layout_marginBottom="50dp"android:layout_marginRight="50dp"/></RelativeLayout>?????????????? 2.控件與控件間位置屬性
?????????????????????控件與控件之間的位置屬性,是指控件與相鄰控件的位置設(shè)置,主要有下面屬性
?????????????????????? android:layout_below="@+id/textView1"??? 該控件位于指定控件的下方
????????????????? ??? android:layout_toLeftOf="@+id/textView1"??? 控件位于指定控件的左側(cè)
???????????????????? ?android:layout_toRightOf="@+id/textView1"??? 控件位于指定控件的右側(cè)
??????????????????? ?android:layout_above="@+id/textView1"?????????? 控件位于指定控件的上面
???????????????????? android:layout_alignBaseline="" 該控件的內(nèi)容與指定控件的內(nèi)容在同一直線上
??????????????????? android:layout_alignBottom=""該控件的底部與指定控件的底部對齊
????????????????? ? android:layout_alignLeft="" 該控件與指定控件左側(cè)對齊
????????????????? ? android:layout_alignRight="" 該控件與指定控件右側(cè)對齊
??????????????????? android:layout_alignTop="" 該控件與指定控件的頂部對齊
???????????????????????
???????????????????????
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="110dp" android:id="@+id/layone"><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFD700"android:text="標簽1" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/textView1"android:background="#FF0000"android:text="標簽2" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="120dp"android:layout_below="@+id/layone"><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#EE9572"android:text="標簽3" /><TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="60dp"android:layout_toRightOf="@+id/textView3"android:background="#CDAA7D"android:text="標簽4" /></RelativeLayout></RelativeLayout>?????????????? 3.商品列表演示樣例
以下我們展示一個商品列表,使用RelativeLayout來展示,效果圖例如以下 ????????????????????? 代碼 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="120dp"android:id="@+id/layout1"><ImageViewandroid:id="@+id/imageView1"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:src="@raw/pad" /><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignTop="@+id/imageView1"android:layout_marginTop="10dp"android:layout_toRightOf="@+id/imageView1"android:text="商品名稱:IPAD AIR" /><TextViewandroid:id="@+id/textView2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:layout_toRightOf="@+id/imageView1"android:layout_below="@+id/textView1"android:text="商品價格:$19" /><TextViewandroid:id="@+id/textView3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:layout_toRightOf="@+id/imageView1"android:layout_below="@+id/textView2"android:text="商品顏色:白色" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="3dp"android:background="#CDAA7D"android:layout_below="@+id/layout1"android:id="@+id/layout2"></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="120dp"android:layout_below="@+id/layout2"android:id="@+id/layout3"><ImageViewandroid:id="@+id/imageView2"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:src="@raw/pad" /><TextViewandroid:id="@+id/textView4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignTop="@+id/imageView2"android:layout_marginTop="10dp"android:layout_toRightOf="@+id/imageView2"android:text="商品名稱:IPAD AIR" /><TextViewandroid:id="@+id/textView5"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:layout_toRightOf="@+id/imageView2"android:layout_below="@+id/textView4"android:text="商品價格:$19" /><TextViewandroid:id="@+id/textView6"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:layout_toRightOf="@+id/imageView2"android:layout_below="@+id/textView5"android:text="商品顏色:白色" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="3dp"android:background="#CDAA7D"android:layout_below="@+id/layout3"></RelativeLayout></RelativeLayout>
總結(jié)
以上是生活随笔為你收集整理的.Net程序猿玩转Android开发---(7)相对布局RelativeLayout的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android非UI线程更新UI的几种方
- 下一篇: 一个月时间整理《深入浅出Node.js》