2nd year Pokemon trainer SE android application development summary ~ 2nd day ~

Introduction

It is the second day of android development summary. This time, I will explain how to use ** Activity # setContentView ** and ** \ <merge > **. I couldn't explain how to handle the layout properly, so I studied.

Activity#setContentView This usage example

sample


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);

The pattern and usage of function calls are as follows. 1. setContentView (View view, ViewGroup.LayoutParams params)     Set the activity content to an explicit view. 2. setContentView (View view)     Set the activity content to an explicit view. 3. setContentView (int layoutResID)     Set the activity content from a layout resource. Reference: https://developer.android.com/reference/android/app/Activity

In 1.2, the View passed as an argument is reflected in the layout. ** ** 3 is the pattern used this time. ** Make Activity read the layout file passed as an argument. ** ** An image that loads xml with setContentView for the actual object of the activity. I was wondering if I had to execute this function in a manner, but I could do it without it. If setContentView (int layoutResID) is not executed in onCreate, the layout is not loaded, so the white screen you want to see in the image below is displayed. スクリーンショット 2019-01-03 16.11.04.png

1.2 is to display additional views later You can think of 3 as being used to display the base Activity first.

<merge> ** Use the include xml to merge with the parent View. ** ** ** If you think "What's the deal?" **, it's the same as me. I managed to understand it, so I will explain it.

First, as a prerequisite, the parent layout on the include side requires a view group. Therefore, if it is super-simplified, it should have the following configuration.

parent.xml


<LinearLayout>
    <include~~~~~/>
</LinearLayout>

And this \ <include ~~~~~ / > will contain the child layout. At this time, if you use a view group instead of \ <merge >, it will look like this.

childLayout1.xml


<LinearLayout>
    <TextView~~~~~/>
    <TextView~~~~~/>
    <TextView~~~~~/>
</LinearLayout>

And when it is included and the union is completed, it will be as follows.

mergedLayout1.xml


<LinearLayout>
    <LinearLayout>
        <TextView~~~~~/>
        <TextView~~~~~/>
        <TextView~~~~~/>
    </LinearLayout>
</LinearLayout>

Did you notice? There is a useless LinearLayout in the LinearLayout, and it's not very cool.

The child layout when using \ <merge > is as follows.

childLayout2.xml


<merge>
    <TextView~~~~~/>
    <TextView~~~~~/>
    <TextView~~~~~/>
</merge>

And coalesce! !!

mergedLayout2.xml


<LinearLayout>
    <TextView~~~~~/>
    <TextView~~~~~/>
    <TextView~~~~~/>
</LinearLayout>

It will be like this. There is no waste from the previous layout. \ <merge > is like this ** used to be merged with the parent ViewGroup **.

Summary

Today was about ** Activity # setContentView ** and ** \ <merge > **. If you look it up again, there are many things that you are interested in. I will continue to investigate and summarize what I wondered with android ~. Thank you for reading.

Recommended Posts

2nd year Pokemon trainer SE android application development summary ~ 2nd day ~
2nd year Pokemon trainer SE android application development summary ~ 1st day ~
Android development link summary
Android application development preparation 7/15
Introduction to Android application development
Notes for Android application development beginners
A memorandum for android application development beginners