CS计算机代考程序代写 android gui Java javaFx Mobile and Ubiquitous Computing

Mobile and Ubiquitous Computing
Labsheet 3 – Layouts
In this labsheet, we’ll be looking at Android’s layout models. Last week, some of you might have run into problems with all of your elements ending up piled on top of each other. Or you might have found that elements did not end up where the visual building interface made it seem like they would. We’re going to have a look at how you can finely control the layout of items on the screen.
Layouts in Android
You should have encountered layouts previously in Java with JavaFX/Swing/AWT. Like all GUI widget toolkits, Android has a number of ways of specifying how widgets should be placed on the screen and how these widgets should behave as the screen expands, shrinks or rotates. There is extensive documentation on the different ways of managing layouts in Android. We don’t have time to explore all of them here, but some of them should feel familiar from similar layout concepts in Java’s GUI toolkits.
OK, let’s go back to last week, where we had an activity that contained a Button, TextView and Spinner. First, rename your project to week4lab and refactor appropriately (alternatively, where
you see week4lab in the code, replace it with week3lab or week2_lab, whatever you have at the moment.) If you created an Empty Activity then, Android will have created your activity using the
Constraint layout. Look at the activity’s text view and on the second line you will see:
It’s also visible in the design view:
LinearLayout
The advantage of using ConstraintLayout is that it allows for complex responsive UI designs without having a bewildering number of groupings for elements. But it’s a bit tricky to get into, so
let’s go with something a little simpler to start, the LinearLayout. To do this, switch to the text view for your activity and replace android.support.constraint.ConstraintLayout with

LinearLayout. Now switch back to design view. You’ll see that things have changed, and your three elements are now arranged in three columns.
In our case, it makes sense for our three elements to be arranged top to bottom, rather than left-to- right. In text view, edit the XML so that the definition includes android:orientation=“vertical”. It should look something like this:
Currently, each of the Views you have in this XML activity have attributes that are specifically for constraint layouts. In this instance the attributes are not incompatible (changing layouts can often cause your app to stop compiling), but the existing attributes will stop our linear layout working properly. You will need to remove each attribute beginning app:layout_constraint from the
Spinner, TextView and Button (e.g., app:layout_constraintLeft_toLeftOf=“parent” etc). Your views should be defined like this:


Leave a Reply

Your email address will not be published. Required fields are marked *