android Activity相對布局的使用方法_Android

來源:互聯網
上載者:User

相對布局要比前面講的線性布局和表格版面配置要靈活一些,所以平常用得也是比較多的。相對布局控制項的位置是與其周圍控制項的位置相關的,從名字可以看出來,這些位置都是相對的,確定出了其中一個控制項的位置就可以確定另一個控制項的位置了。
本次實驗就是顯示如下的activity:


    
其中只有2個button,1個textview,1個edittext。

在相對布局中,一般用到的控制項屬性解釋如下:
在相對布局中有如下屬性,解釋如下:
android:layout_above  為將該控制項的底部放在指定id控制項的上方
android:layout_below   同理類似,將該控制項的頂部放在指定id控制項的下方
android:layout_toLeftOf  將該控制項的右端緊挨著放在指定id控制項的左端。
android:layout_toRightOf  將該控制項的左端緊挨著放在指定id控制項的右端
android:layout_alignParentRight  為true時將該控制項右端與父控制項右端對齊
android:layout_alignParentLeft       為true時將該控制項左端與父控制項左端對齊
android:layout_alignParentTop       為true時將該控制項頂端與父控制項頂端對齊
android:layout_alignParentBottom  為true時將該控制項底端與父控制項底端對齊
android:layout_alignBottom   將該控制項底部與指定id控制項底部控制項對齊
android:layout_alignLeft 將該控制項左邊與指定id控制項左邊對齊
android:layout_alignRight        將該控制項右邊與指定id控制項右邊對齊
android:layout_alignTop  將該控制項頂端與指定id控制項頂端對齊

實現上面activity比較簡單,其xml代碼如下:

複製代碼 代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10px" >

    <TextView
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/input_dis"
        tools:context=".MainActivity" />

       <EditText
  android:id="@+id/edit"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
        android:layout_below="@id/input"
        android:background="@android:drawable/editbox_background"
        />

       <Button
  android:id="@+id/ok"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout_below="@id/edit"
  android:layout_alignParentRight="true"
  android:layout_marginLeft="10px"
  android:text="@string/ok" 
  />

       <Button
  android:id="@+id/cancel"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout_below="@id/edit"
  android:layout_toLeftOf="@id/ok"
  android:text="@string/cancel"

  />

</RelativeLayout>



 總結:activity的相對布局比較靈活,一些常見的屬性也比較多,用得多自然就會了。

 
 作者:tornadomeet

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.