- Hands-On Android UI Development
- Jason Morris
- 353字
- 2021-07-02 23:26:07
Creating a form layout
Once you have a good wireframe to work from, you'll want to start developing the user interface screen. For this, we'll use Android Studio and its wonderful layout editor.
Since this is a brand new project, you'll need to open Android Studio and use File | New | New Project to get it started. Then, follow these steps:
- Name the project Claim, and leave any non-Java support turned off.
- Target Android 4.1 on Phone & Tablet only.
- In the Activity Gallery, choose the Basic Activity:

- Name the new Activity CaptureClaimActivity, and then change the title to Capture Claim. Leave the other parameters at their default values:

- Finish the New Project wizard, and wait for the project to be generated.
- When the project has been generated and synchronized, Android Studio will open the content_capture_claim.xml file in its layout editor.
- By default, Android Studio assumes that you will be using a ConstraintLayout as the root of your layout. This is an incredibly powerful and flexible tool, but also not well suited as the root element of this user interface. You'll need to switch over to the Text view at the bottom of the screen in order to change to something more suitable:

- The file will currently have something like the following XML in place:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.packtpub.claim.CaptureClaimActivity"
tools:showIn="@layout/activity_capture_claim">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
- Change the ConstraintLayout to a simple LinearLayout. LinearLayout is one of the simplest layouts available on Android. It renders each of its children in a straight line, either horizontal or vertical, depending on its orientation attribute. Replace the whole of the content_capture_claim.xml file with the following content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.packtpub.claim.CaptureClaimActivity"
tools:showIn="@layout/activity_capture_claim">
</LinearLayout>
Choosing the right layout to use is about more than just keeping your code simple; less flexible layouts are much faster at runtime and lead to a much smoother user experience. Try to stick to simpler layouts where possible, but also avoid nesting layouts too deep (one inside the other), as this also leads to performance problems.
- Change back to the Design view in the layout editor, and you'll notice that the Component Tree to the left of the design view now has a LinearLayout (vertical) as its only component.
推薦閱讀
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Learn Swift by Building Applications
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓(xùn)版)
- 軟件測試技術(shù)指南
- Visual FoxPro程序設(shè)計習(xí)題集及實驗指導(dǎo)(第四版)
- Learning Probabilistic Graphical Models in R
- PLC應(yīng)用技術(shù)(三菱FX2N系列)
- SSM開發(fā)實戰(zhàn)教程(Spring+Spring MVC+MyBatis)
- C++20高級編程
- Python Interviews
- Django Design Patterns and Best Practices
- 物聯(lián)網(wǎng)系統(tǒng)架構(gòu)設(shè)計與邊緣計算(原書第2版)
- MATLAB 2020 GUI程序設(shè)計從入門到精通
- Roslyn Cookbook