wasup

Android) 요소배치, 클릭시 이미지 변경 본문

IT/Android

Android) 요소배치, 클릭시 이미지 변경

wasupup 2021. 8. 4. 18:08
반응형

new project : hello07_yc

 

수정 : activity_main.xml


철수텍스트의 오른쪽에 영희텍스트 붙이기


activity_main.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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        
    </RelativeLayout>

</LinearLayout>

<?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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:id="@+id/cs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textSize="30dp"
            android:textColor="#121B6A"
            android:text="철수"
            />

        <TextView
            android:id="@+id/yh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textSize="30dp"
            android:textColor="#7A2493"
            android:text="영희"

            android:layout_toRightOf="@+id/cs"
            />

    </RelativeLayout>

</LinearLayout>

android:layout_toRightOf="@+id/cs" : id의 오른쪽

 

 

 


 

 

 

new project : hello08

 

수정 : activity_main.xml

수정 : MainActivity.java



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn01"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="버튼1"
            android:textSize="12pt"
            />

        <Button
            android:id="@+id/btn02"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="버튼2"
            android:textSize="15pt"
            />

        <Button
            android:id="@+id/btn03"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:text="버튼3"
            android:textSize="12pt"
            />
    </LinearLayout>

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/img01"

        />


</RelativeLayout>

android:scaleType="fitCenter" : 이미지를 가운데 배치


MainActivity.java

package com.example.hello08;

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 {
    //변수
    Button b1,b2,b3;
    ImageView imgView;

    Integer imgs[]={R.drawable.img01, R.drawable.img02, R.drawable.img03};

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

        b1=(Button)findViewById(R.id.btn01);
        b2=(Button)findViewById(R.id.btn02);
        b3=(Button)findViewById(R.id.btn03);

        imgView=(ImageView)findViewById(R.id.img);

        b1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                imgView.setImageResource(imgs[0]);//배열
            }
        });//event-end
        b2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                imgView.setImageResource(imgs[1]);//배열
            }
        });//event-end
        b3.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                imgView.setImageResource(imgs[2]);//배열
            }
        });//event-end
    }//onCreate-end
}//class-end

 

 

 

 


 

 

 

 

 

반응형
Comments