ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 직접 풀어보기 5-2
    Android Studio를 활용한 안드로이드프로그래밍 9판 2024. 5. 3. 04:33

    결과창

     

     

    메인 액티비티

    package com.jieun.doitmyself5_2;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ImageView imgView = findViewById(R.id.imgView);
            Button imgBtn = findViewById(R.id.imgBtn);
            imgBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    imgView.setImageResource(R.drawable.maru);
                }
            });
        }
    }

     

    레이아웃

    <?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"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <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="#d1180b"/>
            <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="#ffd400"/>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:background="#000000">
    
                    <Button
                        android:id="@+id/imgBtn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="킹율 나와라" />
                </LinearLayout>
    
            </LinearLayout>
    
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0019f4"
            android:gravity="center">
            <ImageView
                android:id="@+id/imgView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    
    
    </LinearLayout>
Designed by Tistory.