Android UI控制元件系列:RelativeLayout(相對佈局)
RelativeLayout是一個在相對位置上顯示子View元素的VeiwGroup,一個檢視的位置,可以指定為相對於兄妹的元素(比如一個給定的與孫的左邊或者下邊)或者心愛那個對於RelativeLayout區域的位置(比如與底部對齊,剩下的中心)
一個RelativeLayout是一個非常強大使用的為設定使用者介面的佈局,因為它可以消除巢狀的檢視組ViewGroup,如過你發現你用了幾個巢狀的LinearLayout組,你可以替換為一個單獨的RelativeLayout
1、開始一個新的工程,名字叫做HelloRelativeLayout
2、開啟res/layout/main.xml檔案並且插入如下資訊
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="OK" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> </RelativeLayout>
3、注意到每一個android:layout_*屬性,比如layout_below,layout_alignParentRightRight,和layout_toLeftOf,當用一個RelativeLayout的時候,你可以用這些屬性來描述你想要的每個檢視View的位置,每一個這些屬性都定義一個不懂種類的相對位置,一些屬性用到同級檢視的資源ID來定義自己的相對位置。比如最後一個Button是被定義到位於被定義ID為ok的檢視的左邊和頂部對齊,所有的layout佈局屬性都被定義在RelativeLayout.LayoutParams中
4、現在開啟HelloLinearLayout.java並且確定它已經在onCreate()方法中載入了res/layout/main.xml佈局檔案
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
5、你可以看到如下的佈局
相關文章
- Android入門教程 | UI佈局之RelativeLayout 相對佈局AndroidUI
- android佈局------RelativeLayout(相對佈局)詳解Android
- RelativeLayout相對佈局
- Android 相對佈局RelativeLayout 程式碼示例Android
- Xamarin.Forms的相對佈局RelativeLayoutORM
- Android Layout 之 RelativeLayout,程式碼實現相對佈局Android
- Android UI控制元件系列:TableLayout(表格佈局)AndroidUI控制元件
- Android UI系列-----RelativeLayout的相關屬性AndroidUI
- Android UI控制元件系列:GridView(網格佈局)AndroidUI控制元件View
- Android UI控制元件系列:LinearLayout(線性佈局)AndroidUI控制元件
- 佈局管理器——相對佈局
- android 相對佈局,程式碼建立imageview,佈局居中問題AndroidView
- DependentLayout相對佈局
- Flutter學習指南:UI佈局和控制元件FlutterUI控制元件
- Android UI控制元件系列:Dialog(對話方塊)AndroidUI控制元件
- Flutter學習之”相對佈局“Flutter
- Android UI控制元件系列:Toast(提示)AndroidUI控制元件AST
- Android入門教程 | UI佈局之LinearLayout 線性佈局AndroidUI
- SAP UI5 Form 控制元件的 ColumnLayout 佈局特性UIORM控制元件
- Android學習之 UI佈局優化AndroidUI優化
- Android控制元件佈局屬性全解Android控制元件
- Android UI控制元件系列:Button(按鈕)AndroidUI控制元件
- Android UI控制元件系列:TextView(文字框)AndroidUI控制元件TextView
- 阿里Android開發規範:UI 與佈局阿里AndroidUI
- Android UI控制元件系列:Spinner(下拉選單)AndroidUI控制元件
- Android UI控制元件系列:ProgressBar(進度條)AndroidUI控制元件
- css佈局系列1——盒模型佈局CSS模型
- 程式碼設定RelativeLayout相對位置設定
- Android UI控制元件系列:Gallery(畫廊檢視)AndroidUI控制元件
- Android UI控制元件系列:TabWidget(切換卡)AndroidUI控制元件
- Android UI控制元件系列:WebView(網路檢視)AndroidUI控制元件WebView
- Android UI控制元件系列:AutoCompleteTextView(自動提示)AndroidUI控制元件TextView
- android基礎學習-android篇day15-相對佈局的常用屬性Android
- Android 佈局Android
- HarmonyOS Java UI之DirectionalLayout佈局JavaUI
- css經典佈局系列一——垂直居中佈局CSS
- Android UI控制元件系列:Tab Layout(選項卡布局)AndroidUI控制元件
- UICollectionView左對齊流水佈局、右對齊流水佈局UIView