간단하게 Button 3개와 FrameLayout을 이용하여 Tab을 만들어 보았습니다.

가장 위에 TAB1, TAB2, TAB3 버튼이 있고 각 버튼을 클릭했을 때 클릭한 버튼의 번호와 일치하는 Content 화면을 앞으로 가져옵니다.

// TestActivity
package com.example.practicetab;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.tab1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.content1).bringToFront();
            }
        });
        findViewById(R.id.tab2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.content2).bringToFront();
            }
        });
        findViewById(R.id.tab3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.content3).bringToFront();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button android:id="@+id/tab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Tab1"
            />
        <Button android:id="@+id/tab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Tab2"
            />
        <Button android:id="@+id/tab3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Tab3"
            />
    </LinearLayout>
    <FrameLayout android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tab_layout">
        <TextView android:id="@+id/content3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="#0000ff"
            android:textSize="50sp"
            android:textColor="#ffffff"
            android:text="Content3"
            />
        <TextView android:id="@+id/content2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="#00ff00"
            android:textSize="50sp"
            android:textColor="#ffffff"
            android:text="Content2"
            />
        <TextView android:id="@+id/content1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="#ff0000"
            android:textSize="50sp"
            android:textColor="#ffffff"
            android:text="Content1"
            />
    </FrameLayout>
</RelativeLayout>

정렬 관련 속성값을 조금 조절하면 세로텝으로 변경도 가능합니다.

'개발 > android' 카테고리의 다른 글

Hello World  (0) 2020.04.03

+ Recent posts