官术网_书友最值得收藏!

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:

  1. Name the project Claim, and leave any non-Java support turned off.
  2. Target Android 4.1 on Phone & Tablet only.
  3. In the Activity Gallery, choose the Basic Activity:
  1. Name the new Activity CaptureClaimActivity, and then change the title to Capture Claim. Leave the other parameters at their default values:
  1. Finish the New Project wizard, and wait for the project to be generated.
  2. When the project has been generated and synchronized, Android Studio will open the content_capture_claim.xml file in its layout editor.
  1. 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:
  1. 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>
  1. 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.

  1. 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.
主站蜘蛛池模板: 恩平市| 定远县| 马尔康县| 清水河县| 凤山市| 台江县| 台东县| 潮安县| 辽中县| 重庆市| 陇川县| 章丘市| 南部县| 友谊县| 汉寿县| 若尔盖县| 五家渠市| 崇仁县| 滦南县| 即墨市| 定南县| 长宁县| 荃湾区| 和顺县| 云梦县| 桃园县| 塔城市| 泽库县| 石林| 本溪市| 长春市| 黑河市| 额敏县| 新邵县| 阆中市| 潢川县| 松阳县| 长岛县| 双牌县| 栾川县| 永顺县|