개발일지/안드로이드 JAVA

[안드로이드 프로그래밍 7판] 예제 6-15,16 ViewFlipper

꾸주니=^= 2023. 1. 31. 19:00

258,259p

예제 6-15,16 ViewFlipper


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    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="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnPrev"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="이전화면"
                android:textSize="25dp"
                />

            <Button
                android:id="@+id/btnNxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="다음화면"
                android:textSize="25dp" />
        </LinearLayout>
        <ViewFlipper
            android:id="@+id/viewFlipper1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black">

            </LinearLayout>

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

            </LinearLayout>

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

            </LinearLayout>
        </ViewFlipper>
    </LinearLayout>

 

MainActivity.java

package com.example.test14;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    Button btnPrev, btnNxt;
    ViewFlipper vFlipper;

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

        btnPrev = (Button) findViewById(R.id.btnPrev);
        btnNxt = (Button) findViewById(R.id.btnNxt);
        vFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

        btnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.showPrevious();
            }
        });

        btnNxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.showNext();
            }
        });
    }
}

 

[실행 화면]


[헷갈렸던 부분]

xml에서 viewflipper 부분이 계속 안나와서 match, wrap 부분을 계속 바꿔가며 했더니

결론적으로 전체적인 linear 화면 부분을 match_parent로 하고 버튼 부분은 wrap_content와 weight=1로 설정하니 됐다..!

아무튼 이번에도 성공..!