Android Studio를 활용한 안드로이드 프로그래밍 P264 직접 풀어보기 6-3


탭호스트를 이용하여 동물 선택 앱을 작성하라.


- 탭 위젯을 아래쪽에 배치하고 탭은 4개가 나오게한다.

- 프레임레이아웃 안에 리니어레이아웃이아닌 이미지뷰를 배치한다.

HINT: 프레임레이아웃의 layout_weight 속성을 1로한다.




XML코드

탭 위젯을 아래에 배치하려면 탭위젯 코드문의 위치를 프레임레이아웃코드 위에서 아래로 바꿔야합니다.

그런데, 이렇게만하고 실행하면 위젯이 보이지않습니다. (즉, 짤립니다.)

탭 위젯을 정상적으로 아래쪽에 배치하려면 힌트를 이용해야합니다.

프레임레이아웃안의 layout_weight속성을 1로 작성하면 탭위젯을 아래에 배치할 수 있습니다.

(이유는 모르겠는데 이런 형식을 이용할 때 거의 필수옵션이라고합니다.)

<?xml version="1.0" encoding="utf-8"?>
<TabHost 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"
android:id="@android:id/tabhost">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RAB"
android:src="@drawable/rab"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/CAT"
android:src="@drawable/cat"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LION"
android:src="@drawable/lion"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/DOG"
android:src="@drawable/dog"/>
</FrameLayout>
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:background="#f0f000"/>
</LinearLayout>

</TabHost>



MainActivity

XML코드에서 탭위젯의 위치조정하는 작업말고는 다른게 없습니다.

맨아래 tabHost.setCurrentTab(0)의 의미는 최초의 시작화면을 0번째(DOG)화면으로 띄워놓겠다는 의미입니다.

package com.example.practice6_3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=getTabHost();
TabHost.TabSpec tabSpecdog=tabHost.newTabSpec("dog").setIndicator("강아지");
tabSpecdog.setContent(R.id.DOG);
tabHost.addTab(tabSpecdog);
TabHost.TabSpec tabSpeccat=tabHost.newTabSpec("cat").setIndicator("고양이");
tabSpeccat.setContent(R.id.CAT);
tabHost.addTab(tabSpeccat);
TabHost.TabSpec tabSpecrab=tabHost.newTabSpec("rab").setIndicator("토끼");
tabSpecrab.setContent(R.id.RAB);
tabHost.addTab(tabSpecrab);
TabHost.TabSpec tabSpeclion=tabHost.newTabSpec("lion").setIndicator("사자");
tabSpeclion.setContent(R.id.LION);
tabHost.addTab(tabSpeclion);
tabHost.setCurrentTab(0);
}
}


'Android Studio를 활용한 안드로이드 프로그래밍' 카테고리의 다른 글

직접 풀어보기 7-2  (4) 2020.05.19
직접 풀어보기 7-1  (0) 2020.05.18
직접 풀어보기 6-2  (2) 2020.05.04
직접 풀어보기 6-1  (1) 2020.05.03
직접 풀어보기 5-5  (0) 2020.05.03

+ Recent posts