짱짱해커가 되고 싶은 나

05-2. 레이아웃 연습 본문

카테고리 없음

05-2. 레이아웃 연습

동로시 2021. 2. 17. 13:52

연습1 : 중복리니어레이아웃

연습1

<?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_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

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

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

            </LinearLayout>
        </LinearLayout>

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

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

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

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

        </LinearLayout>
    </LinearLayout>

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


</LinearLayout>

 

연습2 : 상대레이아웃

연습2

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/base"
        android:layout_margin="5dp"
        android:text="base"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num1"
        android:layout_alignTop="@+id/base"
        android:layout_toLeftOf="@+id/base"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num2"
        android:layout_alignBaseline="@+id/base"
        android:layout_toLeftOf="@+id/base"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num3"
        android:layout_toLeftOf="@+id/base"
        android:layout_alignParentBottom="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num4"
        android:layout_alignLeft="@+id/base"
        android:layout_alignParentTop="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num5"
        android:layout_alignRight="@+id/base"
        android:layout_alignParentBottom="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num6"
        android:layout_above="@id/base"
        android:layout_toRightOf="@+id/base"/>

</RelativeLayout>

 

연습 3: gravity

각 레이아웃을 누르면 해당하는 레이아웃의 width, height 토스트로 출력

 

연습3

<?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:background="#ff00ff"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/lay_250"
        android:background="#9A2EFE"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:gravity="center">

        <LinearLayout
            android:id="@+id/lay_150"
            android:gravity="center"
            android:background="#ffff00"
            android:layout_width="150dp"
            android:layout_height="150dp">

            <LinearLayout
                android:id="@+id/lay_50"
                android:background="#000000"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:orientation="horizontal">
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>


</LinearLayout>
package com.example.widget4;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    LinearLayout lay1, lay2, lay3;
    Integer width, height;

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

        lay1 = (LinearLayout)findViewById(R.id.lay_250);
        lay2 = (LinearLayout)findViewById(R.id.lay_150);
        lay3 = (LinearLayout)findViewById(R.id.lay_50);

        lay1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = lay1.getWidth();
                height = lay1.getHeight();
                Toast.makeText(getApplicationContext(),
                        "width: " + width.toString() + ", height: " + height.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });

        lay2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = lay2.getWidth();
                height = lay2.getHeight();
                Toast.makeText(getApplicationContext(),
                        "width: " + width.toString() + ", height: " + height.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });

        lay3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                width = lay3.getWidth();
                height = lay3.getHeight();
                Toast.makeText(getApplicationContext(),
                        "width: " + width.toString() + ", height: " + height.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

연습4 : 중복 리니어레이아웃 XML없이 Java 로만구성

연습4

package com.example.widget4;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
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 base_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        LinearLayout baseLayout = new LinearLayout(this);
        baseLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(baseLayout, base_params);

        LinearLayout.LayoutParams bottom_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        LinearLayout bottomLayout = new LinearLayout(this);
        bottomLayout.setBackgroundColor(Color.BLUE);

        LinearLayout.LayoutParams top_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        LinearLayout topLayout = new LinearLayout(this);
        topLayout.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams left_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        LinearLayout leftLayout = new LinearLayout(this);
        leftLayout.setBackgroundColor(Color.RED);

        LinearLayout.LayoutParams right_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        LinearLayout rightLayout = new LinearLayout(this);
        rightLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams y_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
        LinearLayout yLayout = new LinearLayout(this);
        yLayout.setBackgroundColor(Color.YELLOW);

        LinearLayout.LayoutParams b_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
        LinearLayout bLayout = new LinearLayout(this);
        bLayout.setBackgroundColor(Color.BLACK);

        baseLayout.addView(topLayout, top_params);
        topLayout.addView(leftLayout, left_params);
        topLayout.addView(rightLayout, right_params);
        rightLayout.addView(yLayout, y_params);
        rightLayout.addView(bLayout, b_params);
        baseLayout.addView(bottomLayout, bottom_params);

    }
}

처음에 모든 리니어 레이아웃에 동일하게 setContentView(), addView()를 했더니 안됐다

setContentView()를 찾아보니까 이거는 한 번만 사용되야 했다. 최종 뷰 지정 같은 느낌??

그리고 addVeiw에서 레이아웃만 쓰는게 아니라 (레이아웃, 파라미터) 이렇게 고치면 됐다.

(여기서는 params를 다다르게 만들었는데 어차피 내용이 똑같아서 파라미터 하나 만들고 통일하는게 좋을 것 같다)

 

java로 중첩레이어아웃을 만들 때 중요한 부분은 addView의 순서인 것 같다.

Comments