본문 바로가기

programming/안드로이드 스튜디오

[안드로이드 프로그래밍 연습문제] Chapter 5 - 4번 (weigth 속성)

반응형

1. 문제 안내, 예시 화면


 

연습문제 5-4

 위의 화면을 XML파일로 만드시오. 단, 리니어레이아웃만 사용하고 각 렝아웃의 색상을 다르게 설정한다.


 이 카테고리는 학교 강의에서는 풀지 않은 연습문제들의 풀이를 올리는 과제이다. 물론 실습문제들만을 올릴것이다. 연습문제가 아닌 직접 풀어보기와 같은 문제들도 올릴예정이다. 많은 글을 올렸으면 좋겠다.

 

2. 문제풀이

 

 이 문제는 간단하다. 글로 설명하자면 레이아웃안에 최대 세개의 LinearLayout이 중첩된다고 생각하면 된다. 여기서 조금 어려운 부분은 바로 오른쪽 상단 부분이다. 긴 막대기 형태로 세개의 레이아웃이 배열되어 있는데 처음 보면 막막할 것 같다는 생각이 들 수 있다. 다만 weight속성과 LinearLayout의 orientation속성을 알고있다면 잘 해결될 수 있다. 솔직히 말하면 나도 weight속성에 대해 정확하게 알지 못한다. 이 문제도 감으로 대입한 숫자들이 우연하게 들어맞아 해결할 수 있었다. weight에 관해서는 더욱 자세하게 공부하여 이 카테고리에 올릴것이다. 다음은 문제를 해결한 코드와 실행화면이다.

 

<?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="4"
        android:orientation="horizontal">

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

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#FF0000"
            android:orientation="horizontal"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.6"
                android:background="#0000FF"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.6"
                android:background="#00FF00"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.6"
                android:background="#FF0000"
                />


        </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:background="#25C7E0"
        ></LinearLayout>

</LinearLayout>

실행결과


 

반응형