- Android Application Development Cookbook(Second Edition)
- Rick Boyer Kyle Mew
- 333字
- 2021-07-09 19:36:20
Using RelativeLayout
As mentioned in the Introduction, the RelativeLayout
allows Views to be position-relative to each other and the parent. RelativeLayout
is particularly useful for reducing the number of nested layouts, which is very important for reducing memory and processing requirements.
Getting ready
Create a new project and call it RelativeLayout
. The default layout uses a RelativeLayout
, which we will use to align Views both horizontally and vertically.
How to do it...
- Open the
res/layout/activity_main.xml
file and change it as follows:<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Centered" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Below TextView1" android:layout_below="@+id/textView1" android:layout_toLeftOf="@id/textView1" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom Right" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" />
- Run the code, or view the layout in the Design tab
How it works...
This is a very straightforward exercise but it demonstrates several of the RelativeLayout
options: layout_centerVertical
, layout_centerHorizontal
, layout_below
, layout_alignParentBottom
, and so on.
The most commonly used RelativeLayout
layout attributes include:
layout_below
: This View should be below the View specifiedlayout_above
: This View should be above the View specifiedlayout_alignParentTop
: Align this View to the top edge of the parentlayout_alignParentBottom
: Align this View to the bottom edge of the parentlayout_alignParentLeft
: Align this View to the left edge of the parentlayout_alignParentRight
: Align this View to the right edge of the parentlayout_centerVertical
: Center this View vertically within the parentlayout_centerHorizontal
: Center this View horizontally within the parentlayout_center
: Center this View both horizontally and vertically within the parentNote
For the complete list of
RelativeLayout
parameters, visit: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html.
There's more...
In contrast to what we saw earlier, here is an example using a LinearLayout
just to center a TextView
(creating the same effect as the layout_center
parameter of RelativeLayout
):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" > <TextView android:id="@+id/imageButton_speak" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Centered" /> </LinearLayout> </LinearLayout>
Notice this layout is one level deeper than the equivalent RelativeLayout
(which is a LinearLayout
nested within the parent LinearLayout
.) Though a simple example, it's a good idea to avoid unnecessary nesting as it can impact performance, especially when a layout is being repeatedly inflated (such as a ListItem
).
See also
- The next recipe, Using LinearLayout, which will give you an alternative layout
- See the Optimizing layouts with the Hierarchy Viewer recipe for more information on efficient layout design
- R語言數據分析從入門到精通
- Arduino by Example
- Vue.js 3.x從入門到精通(視頻教學版)
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- MySQL數據庫管理與開發(慕課版)
- ADI DSP應用技術集錦
- Kotlin編程實戰:創建優雅、富于表現力和高性能的JVM與Android應用程序
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Scratch·愛編程的藝術家
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- Using Yocto Project with BeagleBone Black
- Web前端開發技術實踐指導教程
- Mastering Unity 2017 Game Development with C#(Second Edition)
- Android Application Programming with OpenCV 3
- Python Geospatial Analysis Cookbook