모바일/android

[안드로이드프로그래밍6판] 5장 직접 풀어보기 5-2, 실습 5-1

2021. 10. 16. 11:35
목차
  1. * xml 파일없이 화면 코딩
728x90
반응형

정답지아닙니다.

블로그 주인이 쓴 답지이므로

오타, 오류 있을 수 있습니다.

(댓글로 알려주시면 정정하겠습니다!)

 

 

직접 풀어보기 5-2
AVD 화면

 

<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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:background="#FF0000">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <LinearLayout
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFF200"
                android:orientation="vertical">
            </LinearLayout>
            <LinearLayout
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@color/black">
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

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

</LinearLayout>

▶ 복습 필요

- 중복 레이아웃 구조 헷갈리지 않게 반복하기

 

 

 


* xml 파일없이 화면 코딩

 

 

실습 5-1
AVD 화면

 

<java 코드> - 그냥 베끼면서 익힘, 나중에 복습 필요

package com.cookandroid.practice5_1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Binder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout baseLayout = new LinearLayout(this);
        baseLayout.setOrientation(LinearLayout.VERTICAL);
        baseLayout.setBackgroundColor(Color.rgb(0,255,0));
        setContentView(baseLayout, params);

        Button btn = new Button(this);
        btn.setText("버튼입니다.");
        btn.setBackgroundColor(Color.MAGENTA);
        baseLayout.addView(btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "코드로 생성한 버튼입니다.",
                Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

직접 풀어보기 5-3
AVD 화면

 

<java 코드>

package com.cookandroid.myself5_3;

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 {

    String result;

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

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout baseLayout = new LinearLayout(this);
        baseLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(baseLayout, params);

        EditText edit = new EditText(this);

        Button btn = new Button(this);
        btn.setText("버튼입니다");
        btn.setBackgroundColor(Color.YELLOW);

        TextView tv = new TextView(this);
        tv.setText("");
        tv.setTextColor(Color.RED);
        tv.setTextSize(20);

        baseLayout.addView(edit);
        baseLayout.addView(btn);
        baseLayout.addView(tv);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result = edit.getText().toString();
                tv.setText(result);
            }
        });
    }
}
저작자표시 비영리 (새창열림)

'모바일 > android' 카테고리의 다른 글

[안드로이드프로그래밍6판] 5장 실습 5-2, 직접 풀어보기 5-5  (0) 2021.10.16
[안드로이드프로그래밍6판] 5장 직접 풀어보기 5-4  (0) 2021.10.16
[안드로이드프로그래밍6판] 4장 연습 문제 7, 8  (0) 2021.10.16
[안드로이드프로그래밍6판] 4장 실습 4-2, 직접 풀어보기 4-4  (0) 2021.10.16
[안드로이드프로그래밍6판] 4장 직접 풀어보기 4-2, 4-3 실습 4-1  (0) 2021.10.15
  1. * xml 파일없이 화면 코딩
'모바일/android' 카테고리의 다른 글
  • [안드로이드프로그래밍6판] 5장 실습 5-2, 직접 풀어보기 5-5
  • [안드로이드프로그래밍6판] 5장 직접 풀어보기 5-4
  • [안드로이드프로그래밍6판] 4장 연습 문제 7, 8
  • [안드로이드프로그래밍6판] 4장 실습 4-2, 직접 풀어보기 4-4
hatch
hatch
250x250
hatch
차근차근 쌓아올리는,
hatch
전체
오늘
어제
  • 분류 전체보기 (121)
    • TIL (3)
    • [JAVA] (17)
      • 생활코딩 (11)
    • 모바일 (25)
      • android (24)
      • ReactNative (1)
    • 웹개발 (25)
      • React (3)
      • jQuery (5)
      • Springboot (2)
    • 알고리즘 (42)
    • [프로그래밍기초지식] (1)
    • [기술문서 읽기] (0)
      • 개념 번역 (0)
    • 인사이트(insight) (2)
    • git (2)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • write
  • manger

공지사항

인기 글

태그

  • 명시적 인텐트
  • TIL
  • jquery
  • 재귀
  • Doit!자바프로그래밍입문
  • 210908
  • 모프2주차
  • 안드로이드프로그래밍6판
  • 깊은복사
  • DP
  • 별찍기
  • 중복리니어레이아웃
  • state
  • 반복문
  • BufferedReader
  • javascript
  • 백준
  • 노이클립스
  • scanf
  • 타일링

최근 댓글

최근 글

hELLO · Designed By 정상우.
hatch
[안드로이드프로그래밍6판] 5장 직접 풀어보기 5-2, 실습 5-1
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.