본문 바로가기
DEVELOPMENT

[Android Studio] Chapter 5 연습문제

by 200% 2021. 6. 28.

[연습문제]

 

4

XML

더보기
<?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"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#ffc92b">
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#000000">
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#000b8d">
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#009d0d">
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#ff2f2d">
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#0067a3">
    </LinearLayout>

</LinearLayout>

 

5

XML

더보기
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/standard"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="기준 위젯" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/standard"
        android:layout_alignParentLeft="true"
        android:layout_toL="@+id/standard"
        android:text="1번" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/standard"
        android:layout_alignParentLeft="true"
        android:text="2번" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/standard"
        android:layout_alignParentBottom="true"
        android:text="3번" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/standard"
        android:layout_alignParentTop="true"
        android:text="4번" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/standard"
        android:layout_alignParentBottom="true"
        android:text="5번" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_above="@+id/standard"
        android:text="6번" />

</RelativeLayout>

 

6

XML

더보기
<?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:layout_gravity="center"
    android:gravity="center"
    android:background="#cb00a4"
    android:id="@+id/layout1"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        android:background="#730089">

        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical"
            android:background="#ffd11f">

            <LinearLayout
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:id="@+id/layout4"
                android:orientation="vertical"
                android:background="#040302">
            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Java

더보기
package com.cookandroid.a56;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    LinearLayout layout1, layout2, layout3, layout4;
    String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("연습문제 5-6");

        layout1 = findViewById(R.id.layout1);
        layout2 = findViewById(R.id.layout2);
        layout3 = findViewById(R.id.layout3);
        layout4 = findViewById(R.id.layout4);

        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result = "Width : "+layout1.getWidth()+", Height : "+layout1.getHeight();
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
            }
        });

        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result = "Width : "+layout2.getWidth()+", Height : "+layout2.getHeight();
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
            }
        });

        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result = "Width : "+layout3.getWidth()+", Height : "+layout3.getHeight();
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
            }
        });


        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result = "Width : "+layout4.getWidth()+", Height : "+layout4.getHeight();
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

7

"안드로이드 스튜디오 연습문제 5장 7번"

"안드로이드 프로그래밍 연습문제 5장 7번"

요건 최근에 추가된 문제인지 관련 답이 업로드된 블로그를 못 봐서 검색해서 들어오시라고 강조해봤다!

Java

더보기
package com.cookandroid.a57;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setTitle("연습문제 5-7");
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);

        LinearLayout baseLayout = new LinearLayout(this);
        baseLayout.setOrientation(LinearLayout.VERTICAL);
        baseLayout.setBackgroundColor(Color.BLUE);
        setContentView(baseLayout, params);
        //1번
        LinearLayout one = new LinearLayout(this);
        one.setOrientation(LinearLayout.HORIZONTAL);

        //빨강
        LinearLayout red = new LinearLayout(this);
        red.setOrientation(LinearLayout.HORIZONTAL);
        red.setBackgroundColor(Color.RED);

        //2번
        LinearLayout two = new LinearLayout(this);
        two.setOrientation(LinearLayout.VERTICAL);

        //노랑
        LinearLayout yellow = new LinearLayout(this);
        yellow.setOrientation(LinearLayout.HORIZONTAL);
        yellow.setBackgroundColor(Color.YELLOW);

        //검정
        LinearLayout black = new LinearLayout(this);
        black.setOrientation(LinearLayout.HORIZONTAL);
        black.setBackgroundColor(Color.BLACK);

        //파랑
        LinearLayout blue = new LinearLayout(this);
        blue.setOrientation(LinearLayout.HORIZONTAL);
        blue.setBackgroundColor(Color.BLUE);

        baseLayout.addView(one, params);
        baseLayout.addView(blue, params);

        one.addView(red, params);
        one.addView(two, params);

        two.addView(yellow, params);
        two.addView(black, params);
    }
}