- Android Application Development Cookbook(Second Edition)
- Rick Boyer Kyle Mew
- 328字
- 2021-07-09 19:36:20
Defining and inflating a layout
When using the Android Studio wizard to create a new project, it automatically creates the res/layout/activity_main.xml
file (as shown in the following screenshot). It then inflates the XML file in the onCreate()
callback with setContentView(R.layout.activity_main)
.

For this recipe, we will create two, slightly different layouts and switch between them with a button.
Getting ready
Create a new project in Android Studio and call it InflateLayout
. Once the project is created, expand the res/layout
folder so we can edit the activity_main.xml
file.
How to do it...
- Edit the
res/layout/activity_main.xml
file so it includes a button as defined here:<Button android:id="@+id/buttonLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left Button" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:onClick="onClickLeft"/>
- Now make a copy of
activity_main.xml
and call itactivity_main2.xml
. Change the button so it matches the following:<Button android:id="@+id/buttonRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Right Button" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:onClick="onClickRight"/>
- Open
MainActivity.java
and add the following two methods to handle the button clicks:public void onClickLeft(View view) { setContentView(R.layout.activity_main2); } public void onClickRight(View view) { setContentView(R.layout.activity_main); }
- Run this application on a device or emulator to see it in action.
How it works...
The key here is the call to setContentView()
, which we have come across before in the autogenerated onCreate()
code. Just pass a layout ID to setContentView()
and it automatically inflates the layout.
This code is meant to make the concept easy to understand but would be overkill for simply changing the property of a Button (in this example, we could just change the alignment on the button click). Inflating the layout is usually needed once, in the onCreate()
method, but there are times when you may want to manually inflate a layout, as we did here. (If you were manually handling orientation changes, it would be a good example.)
There's more...
As well as identifying a layout using a resource ID, as we did here, setContentView()
can also take a View as an argument, for example:
findViewById(R.id.myView) setContentView(myView);
See also
- As mentioned previously, see the Fragment topic, in Chapter 5, Exploring Fragments, AppWidgets, and the System UI, for the alternative method to change the screen layout
- Docker進(jìn)階與實(shí)戰(zhàn)
- Manga Studio Ex 5 Cookbook
- Web Application Development with R Using Shiny(Second Edition)
- 高級(jí)C/C++編譯技術(shù)(典藏版)
- Python自然語(yǔ)言處理(微課版)
- STM32F0實(shí)戰(zhàn):基于HAL庫(kù)開(kāi)發(fā)
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第2版)(微課版)
- Hands-On Automation Testing with Java for Beginners
- Flutter跨平臺(tái)開(kāi)發(fā)入門(mén)與實(shí)戰(zhàn)
- PySpark Cookbook
- PHP 7從零基礎(chǔ)到項(xiàng)目實(shí)戰(zhàn)
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- Arduino機(jī)器人系統(tǒng)設(shè)計(jì)及開(kāi)發(fā)
- HTML5游戲開(kāi)發(fā)實(shí)戰(zhàn)
- 微信小程序開(kāi)發(fā)邊做邊學(xué)(微課視頻版)