Build Android common layout interface

1. Experimental tasks

(1) Mastering writing layouts in XML files and Java code

(2) Mastering common layouts: relative layout, linear layout, frame layout and table layout

2. Experiment content

1. Write layouts in XML files and in Java code

2. Build a common layout

(1) Relative layout

(2) Linear layout

(3) Frame layout

(4) Table layout

3. Experimental steps

1. Writing layouts in XML

2. Write the layout in Java code

2. Build a common layout

1. Relative layout

2. Linear Layout

3. Table Layout

4. Frame layout

5. Use the TableLayout layout to implement a simple calculator interface

The two buttons on the left of the last row have tried many methods and failed to get it right (for example, TableLayout is embedded); in the end, only LinearLayout can be used to set it up.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="C"
            android:layout_alignParentTop="true"
            android:layout_marginTop="270dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="DEL"
            android:layout_alignParentTop="true"
            android:layout_marginTop="270dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="%"
            android:layout_alignParentTop="true"
            android:layout_marginTop="270dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"
            android:layout_alignParentTop="true"
            android:layout_marginTop="270dp"/>
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="x" />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-" />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" />
    </TableRow>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="("
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=")" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="0" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="." />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="=" />
    </LinearLayout>
</TableLayout>

4. Problems encountered during the experiment

1. I encountered several problems when importing the project to Android studio

(1) Android studio reports an error when loading the project (Your project path contains non-ASCII characters. This will most likely cause)

Cause: The project path contains non-ASCII characters. This will most likely cause the build to fail on Windows. Please move the project to another directory.

solve:

That is, in the project's gradle.properties add:

android.overridePathCheck=true

This warning can be disabled by adding the line "android.overridePathCheck=true" to the gradle.properties file in the project directory.

(2) Running the project reports the error "Unable to start the daemon process"

This is a result of low memory for gradle, we need to reset

As shown in the figure, find gradle.properties, add org.gradle.jvmargs=-Xmx1024m and save it. If the file already exists, delete it first. If not, change 1024 to 512

Build succeeded

(3) android studio compilation error: Android resource linking failed

This situation is generally caused by the project configuration. Open File-> Project Structure and see the modules option as follows:

In the above figure, you will find that the compile sdk version and the build tools version are inconsistent, and the build tools version is also changed to 26.

As shown below:

It ran successfully again.

2. When writing layouts in Java, you need to

setContentView(R.layout.activity_main);

Delete this line of code. This line of code means to call the content in activity_main. If it is not deleted, the layout in active_main.xml will always be displayed.

5. Experiment summary

The content of this experiment is my first attempt to get in touch with Android development. The content is not difficult, but adjusting the position of each component is maddening and needs to be adjusted carefully. It is not only to gain new knowledge of Android development, but also to go back and re-consolidate it. Java-related content, the overall harvest of this experiment is quite large

Tags: Android Android Studio programming language

Posted by fareasd on Sat, 10 Sep 2022 23:03:12 +0300