Android:控件布局(相對布局)RelativeLayout


RelativeLayout是相對布局控件:以控件之間相對位置或相對父容器位置進行排列。

 

相對布局常用屬性:

子類控件相對子類控件:值是另外一個控件的id

android:layout_above----------位於給定DI控件之上
android:layout_below ----------位於給定DI控件之下

android:layout_toLeftOf -------位於給定控件左邊
android:layout_toRightOf ------位於給定控件右邊

android:layout_alignLeft -------左邊與給定ID控件的左邊對齊
android:layout_alignRight ------右邊與給定ID控件的右邊對齊
android:layout_alignTop -------上邊與給定ID控件的上邊對齊
android:layout_alignBottom ----底邊與給定ID控件的底邊對齊

android:layout_alignBaseline----對齊到控件基准線

相對父容器,值是true或false
android:layout_alignParentLeft ------相對於父靠左
android:layout_alignParentTop-------相對於父靠上
android:layout_alignParentRight------相對於父靠右
android:layout_alignParentBottom ---相對於父靠下

android:layout_centerInParent="true" -------相對於父即垂直又水平居中
android:layout_centerHorizontal="true" -----相對於父即水平居中
android:layout_centerVertical="true" --------相對於父即處置居中

相對於父容器位置:

android:layout_margin="10dp"
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom

版本4.2以上相對布局新屬性

android:layout_alignStart---------------------將控件對齊給定ID控件的頭部
android:layout_alignEnd----------------------將控件對齊給定ID控件的尾部
android:layout_alignParentStart--------------將控件對齊到父控件的頭部
android:layout_alignParentEnd---------------將控件對齊到父控件的尾部

<?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" >
    
    <TextView 
        android:id="@+id/firstview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一文本"
        android:background="#ff0000"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二文本"
        android:background="#ffff00"
        android:layout_toRightOf="@+id/firstview"
        />
</RelativeLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM