Android-RelativeLayout布局技巧(一)

來源:互聯網
上載者:User

標籤:

 如果有一個需求是這樣的,在標題中的右上方有一個button

 1     <?xml version="1.0" encoding="utf-8"?>   2     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   3         android:layout_width="match_parent"   4         android:layout_height="match_parent"   5         android:orientation="vertical" >   6      <RelativeLayout    7          android:layout_width="match_parent"   8          android:layout_height="45dp"   9          android:background="@color/pink_light">  10          <TextView   11              android:layout_width="wrap_content"  12              android:layout_height="wrap_content"  13              android:text="標題"  14              android:layout_centerInParent="true"  15              />  16            17          <Button   18              android:layout_width="45dp"  19              android:layout_height="26dp"  20              android:layout_alignParentRight="true"  21              android:layout_marginRight="10dp"  22              android:layout_centerVertical="true"  23              android:background="@color/green_light"  24              android:text="更多"  25              android:textSize="12sp"  26              />  27      </RelativeLayout>     28     </LinearLayout>  

其實上面的例子用到了相對布局的相對父控制項,置中,靠右,距離右邊多少db。當然相對父控制項比較多,我們也給你自己定義一個參照物,比如,我們可以相對標題中的TextView作為參照物

 1     <?xml version="1.0" encoding="utf-8"?>   2     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   3         android:layout_width="match_parent"   4         android:layout_height="match_parent"   5         android:orientation="vertical" >   6      <RelativeLayout    7          android:layout_width="match_parent"   8          android:layout_height="45dp"   9          android:background="@color/pink_light">  10          <TextView   11              android:id="@+id/title"  12              android:layout_width="wrap_content"  13              android:layout_height="wrap_content"  14              android:text="標題"  15              android:layout_centerInParent="true"  16              />  17          <TextView   18              android:layout_width="wrap_content"  19              android:layout_height="wrap_content"  20              android:text="標題2"  21              android:layout_toRightOf="@+id/title"  22              android:layout_centerInParent="true"  23              android:layout_marginLeft="5dp"  24              />  25      </RelativeLayout>     26     </LinearLayout>  

 

此篇主要是 相對參照物

Android-RelativeLayout布局技巧(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.