자바 웹 개발자가 될거야/JAVA
[Android] 탭하여 그림 보여주기
whitz
2021. 12. 23. 10:49
- flagm_layout.xml 하나 더 만듦
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dog"
android:layout_gravity="center"
></ImageView>
<TextView
android:id="@+id/tv1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="그림이름"/>
</LinearLayout>
- .java
package com.example.ex6_21;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
ActionBar.Tab tabDog, tabCat, tabRabbit, tabHorse;
MyTabFragment [] myTabFrags = new MyTabFragment[4];
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
MyTabFragment myTabFrag = null;
if(myTabFrags[tab.getPosition()] == null){
myTabFrag = new MyTabFragment();
Bundle data = new Bundle();
data.putString("tabId", tab.getPosition()+"");
myTabFrag.setArguments(data);
myTabFrags[tab.getPosition()] = myTabFrag;
}else myTabFrag = myTabFrags[tab.getPosition()];
ft.replace(android.R.id.content, myTabFrag);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { }
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setTitle("");
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabDog = bar.newTab();
tabDog.setIcon(R.drawable.icon_dog);
tabDog.setTabListener(this);
bar.addTab(tabDog);
tabCat = bar.newTab();
tabCat.setIcon(R.drawable.icon_cat);
tabCat.setTabListener(this);
bar.addTab(tabCat);
tabRabbit = bar.newTab();
tabRabbit.setIcon(R.drawable.icon_rabbit);
tabRabbit.setTabListener(this);
bar.addTab(tabRabbit);
tabHorse = bar.newTab();
tabHorse.setIcon(R.drawable.icon_horse);
tabHorse.setTabListener(this);
bar.addTab(tabHorse);
}
public static class MyTabFragment extends Fragment {
String tabId;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
tabId = data.getString("tabId");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View baseView = inflater.inflate(R.layout.flagm_layout,null);
ImageView iv1 = baseView.findViewById(R.id.iv1);
TextView tv1 = baseView.findViewById(R.id.tv1);
if(tabId.equals("0")) {
iv1.setImageResource(R.drawable.dog);
tv1.setText("강아지 그림");
}else if(tabId.equals("1")) {
iv1.setImageResource(R.drawable.cat);
tv1.setText("고양이 그림");
} else if(tabId.equals("2")) {
iv1.setImageResource(R.drawable.rabbit);
tv1.setText("토끼 그림");
} else if(tabId.equals("3")) {
iv1.setImageResource(R.drawable.horse);
tv1.setText("말 그림");
}
return baseView;
}
}
}
- 상단에 있는 강아지 아이콘 클릭하면 강아지 그림 등장, 고양이 아이콘 클릭하면 고양이 그림 등장. 토끼랑 말도 마찬가지