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

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.
主站蜘蛛池模板: 巴彦淖尔市| 固镇县| 赤水市| 道孚县| 翼城县| 溧水县| 沂水县| 田林县| 六枝特区| 离岛区| 晴隆县| 和田县| 天峻县| 武冈市| 大名县| 阿合奇县| 永清县| 桂平市| 高台县| 潞城市| 皮山县| 铅山县| 白朗县| 会东县| 共和县| 乌审旗| 新乡市| 青州市| 临猗县| 航空| 永城市| 西乡县| 隆化县| 金乡县| 河源市| 南溪县| 大足县| 尼玛县| 佛冈县| 龙泉市| 江山市|