Android development APP portal interface design (Assignment 1)

Keywords: Java Android Android Studio

Android development APP portal interface design (Assignment 1)

1. Content: please implement the APP portal interface framework design according to the course practice, including at least 4 tab pages, which can realize the click switching between tab pages;
2. Technology: use layouts and fragment s to click and listen to controls;

1, Key steps of project development

1.new a project, continue to next, select the save path, and click finish. The save path here should preferably not have spaces, and the file name can also be case sensitive. However, when AS is recognized, it is lowercase, and TYPO warning will appear (but it can not be care), which will have no impact on the subsequent program execution.


2. Operate activity_main.xml. Because a program cannot be a single structure, and it will be troublesome if an error occurs directly in the main XML file, we'd better include our new XML file into activity_main.xml when designing. (after all, it's better to delete it if it's wrong.)

The following are my new top.xml and bottom.xml:

top.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1"
    android:background="#393030">

    <TextView
        android:id="@+id/textView_top"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:textSize="28sp"
        android:textColor="#FD98BA"
        android:text="MyWechat"
        android:gravity="center"
        />
</LinearLayout>


bottom.xml partial code

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:background="#393030">

    <LinearLayout
        android:id="@+id/linearlayout_talk"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView_talk"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:srcCompat="@drawable/talk" />

        <TextView
            android:id="@+id/textView_talk"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="chat"
            android:textAlignment="center"
            android:textColor="#FD90B5" />
    </LinearLayout>

3. Create a fragment to switch pages by clicking the chart below. Right click on your com.example.MyApplication folder to create a new one, as shown in the figure below.

The corresponding xml file will be automatically generated in the layout folder below.

4. Set the interface you want to display in the xml corresponding to each fragment.
For example:

5. Then MainActivity.java is written. For details, please refer to the second core technology.

2, Core technology

*Corresponding technical requirements
1. findViewById() function
It is defined as follows:

public <T extends View> T findViewById(@IdRes int id) {
        return getDelegate().findViewById(id);
    }

Simply put, it is to pass the value of the View in xml to the main function. There are no views in your layout file in the class of the main function, so create an object of the same type as the View you need to reference in the class of the main function, and then establish an association through findViewById(), and then carry out various operations.

2. FragmentManager and FragmentTrasaction:
FragmentManager, as its name implies, is a fragment manager. Second, FragmentTransaction is a type of processing fragments initialized by FragmentManager, also known as transaction.
The initialization process is as follows:

fragmentManager=getSupportFragmentManager();
 FragmentTransaction transaction=fragmentManager.beginTransaction();

fragmentmanager is a member variable in the MainActivity.java class, which can be directly initialized and used in the main function oncreate or other self constructed functions.
Transaction is a dynamic transaction. After each call, you need to use the commit() function to close it. See Transaction and usage in Java
When writing the FragmentManager initialization statement, multiple fragmentmanagers will appear in the AS teleprompter. See:

The two fragmentmanagers come from different packages. If the FragmentManager uses the Android x.fragment.app package, the transaction should not use the android.app package. For this point, please refer to the differences written by this sister required android.app.fragment,required:'android.app.Fragment'

3. Monitor:
There are four ways for Andrews to set up monitoring. For details, see this elder brother's writing Android Development - four ways to set up listeners
It's really detailed!
I use the first, that is, using the interface to realize monitoring.
First of all, MainActivity needs to inherit an onClickListener interface, which can be View.onClickListener or button.onClickListener, depending on the type of control you need to click. After inheritance, an error will be reported. At this time, just insert the onClick function into MainActivity according to the application prompt.
The second is to set a listener in the oncreate function,

Linearlayout1.setOnClickListener(this);

Then, the onclick function automatically generated in the first step is written. The click effect you want, what fragment to switch, and what color the font becomes can be written in the onclick function.
*Display of operation results
[initial interface]
Do you like pink ink@_@

[click each icon below, and the icon will become corresponding fruit when the page is switched]

Well... That's it... It's very simple, isn't it... God knows I've done this for a few days (I admit I'm vegetabledog, crying)

Maybe someone will flash back after opening the app. The logcat log from debug reads:

Do you know why I reported an error? For this error, I don't know how to re-establish several projects!! finally, I found that the teacher changed the name of the imported java package in class.
android.app.Fragment is different from android.appcompat.app.fragment!
The first one can be compatible with multiple versions of Android, and the second one only supports newer versions of Android. but if you use a newer version of Android studio, you'd better use the package starting with Androidx. Otherwise, it will flash back if the code is correct!

Also, don't think that if the Android teleprompter is easy to use, you can close your eyes and press enter when you see the function name. Many functions, such as add, will be overloaded several times. When writing, you should look not only at the function name but also at the parameters. If a novice like me makes mistakes because of this small problem, the consequences will be unimaginable..

It's the first time I've zqsg been blogging, and I suddenly found that blogging is very useful(

Some operation teachers do it step by step when they talk. They think it's nothing. When they think about it, they think there's a lot of knowledge in it (laughter)

To sum up, writing code is not the most annoying thing, but the most annoying thing is the errors caused by various unknown reasons. After they are solved, there are few hairs left. Therefore, be careful when writing files, setting parameters, including naming variables. If there are nonverbal errors, don't panic, and try to look at the logcat log (just look at the first line) , or start all over again. In short, errors do not come out of thin air. They are all caused by a certain operation or a statement that is not written well. On the contrary, since it is an error, there must be a wrong solution. After the error is solved, your realm will rise to a level.

I wish you peace and health!

This is my gitee zsysandroid_toohard

Posted by Rich464 on Tue, 05 Oct 2021 12:29:19 -0700