淺析Android App的相對布局RelativeLayout
一、什么是相對布局
相對布局是另外一種控件擺放的方式
相對布局是通過指定當前控件與兄弟控件或者父控件之間的相對位置,從而達到相對的位置
二、為什么要使用相對布局
相對于線性布局ui性能好
三、相對布局的兩組常用屬性
值為某個存在控件id:
(1)android:layout_below放在某個存在id控件的下邊緣(也就是當前控件的上邊對齊到某個id控件的下邊緣
(2)android:layout_above放在某個存在id控件的上邊緣(也就是當前控件的下邊緣對齊到某個id控件的上邊緣
(3)android:layout_toLeftOf 放在某個存在id控件的左邊(也就是當前控件的右邊對齊到某個id控件的左邊
(4)android:layout_toRightOf 放在某個存在id控件的右邊(也就是當前控件的左邊對齊到某個id控件的右邊)
(5)android:layout_alignLeft 當前的控件左邊緣對齊到某個存在的id控件的左邊緣
(6)android:layout_alignRigth 當前的控件右邊緣對齊到某個存在的id控件的右邊緣
(7)android:layout_alignTop 當前的控件上邊緣對齊到某個存在的id控件的上邊緣
(8)android:alignBottom 當前的控件下邊緣對齊到某個存在的id控件的下邊緣
1.對齊至控件的基準線
基準線是為了保證印刷字母的整齊而劃定的線
值為某個存在控件的id:
android:layout_alignBaseline
2.與父控件的四個邊緣對齊
值為true or false:
(1)android:layout_aliginParentLeft 對齊到父控件的左邊
(2)android:layout_alignParentRight 對錢對齊到父控件的右邊
(3)android:layout_alignParentTop對齊到父控件的上邊
(4)android:layout_alignParentBottom 對齊到父控件的下邊
3.對齊至父控件的中央位置
值為 true or false:
(1)android:layout_centerInParent 對齊到父控件的最中央位置
(2)android:layout_layout_Horizontal 對齊到父控件水平方向中央的位置
(3)android:layout_centerVertical 對齊到父控件垂直方向中央的位置
4.android 4.2 Relativelayout布局的新屬性
值為某個存在控件的di:
(1)android:layout_alignStart 當前控件的起始位置對對齊到某個存在控件id的起始位置
(2)android:layout_alignEnd 當前控件的起始位置對對齊到某個存在控件id的對齊到終點位置
值為true or false:
(1)android:layout_alignParentStart 對齊到父控件的起始位置
(2)android:layout_alignParentEnd 對齊到父控件的終點位置
5.字體居中
android:gravity="center"
android:hint="值"
andriod:inputType="textpassworld"
四、相對布局實例—登錄界面
<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:layout_margin="20dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/Txttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_alignParentRight="true"
android:text="登錄界面"/>
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/Txttitle"
android:layout_alignRight="@id/Txttitle"
android:layout_below="@id/Txttitle"
android:layout_marginTop="20dp"
android:hint="username"/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/username"
android:layout_alignLeft="@id/username"
android:layout_alignRight="@id/username"
android:layout_marginTop="20dp"
android:hint="password"
android:inputType="textCapWords"/>
</RelativeLayout>

相關文章
Android開發(fā)中LayoutInflater用法詳解
這篇文章主要介紹了Android開發(fā)中LayoutInflater用法,結合實例形式分析了LayoutInflater類的功能、作用、使用方法及相關注意事項,需要的朋友可以參考下2016-08-08
Android Compose remember的使用和原理詳解
本文詳細介紹了Android Jetpack Compose中的remember函數(shù),它是優(yōu)化UI組件重組性能的重要工具,remember可以緩存數(shù)據(jù)或?qū)ο?避免每重組時重新計算,保持狀態(tài)不變,通過合理使用`remember`,可以提高應用的性能和流暢度,感興趣的朋友一起看看吧2025-03-03
Android實現(xiàn)打開手機淘寶并自動識別淘寶口令彈出商品信息功能
最近項目經(jīng)理給我們安排一個活兒,基于Android開發(fā)實現(xiàn)打開手機淘寶,并自動識別淘口令,彈出商品信息,今天小編就抽空給大家分享下這個需求是怎么實現(xiàn)的,需要的朋友參考下吧2017-11-11
Android CoordinatorLayout高級用法之自定義Behavior
這篇文章主要介紹了Android CoordinatorLayout高級用法之自定義Behavior,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

