MainActivity를 생성하면 자동으로 activity_main.xml파일이 연결되었다.
그렇다면,
1. 클래스를 만들어서 xml파일을 지정하여 화면에 보여주는 방법과(FrameLayout)
2. xml파일에서 클래스를 지정해주는 방법(fragment)를 살펴보도록 하겠다.
1. activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:weightSum="2"
- android:gravity="center"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <fragment
- android:id="@+id/f"
- android:name="com.example.rhkdg.myapplication.ColorSelect"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- />
- <FrameLayout
- android:id="@+id/fl"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- />
- </LinearLayout>
2. color_select_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_height="match_parent"
- android:weightSum="3">
- <View
- android:id="@+id/v_red"
- android:layout_weight="1"
- android:background="#ff0000"
- android:layout_width="match_parent"
- android:layout_height="0dp"/>
- <View
- android:id="@+id/v_blue"
- android:layout_weight="1"
- android:background="#0000ff"
- android:layout_width="match_parent"
- android:layout_height="0dp"/>
- <View
- android:id="@+id/v_green"
- android:layout_weight="1"
- android:background="#00ff00"
- android:layout_width="match_parent"
- android:layout_height="0dp"/>
- </LinearLayout>
3. color_view_layout.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:gravity="center"
- android:orientation="vertical"
- android:weightSum="3"
- xmlns:app="http://schemas.android.com/apk/res-auto">
- <View
- android:id="@+id/v"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ff0000"
- />
- </LinearLayout>
4. MainActivity.java : ColorSelect의 색상을 클릭하면, FrameLayout-> ColorView를 지정해줌.
- package com.example.rhkdg.myapplication;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v7.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity implements OnColorClickListener{
- ColorView colorView;
- public void onCreate(Bundle saveInstanceState){
- super.onCreate(saveInstanceState);
- setContentView(R.layout.activity_main);
- colorView = new ColorView();
- getSupportFragmentManager().beginTransaction().add(R.id.fl, colorView).commit();
- }
- public void onColorClick(int color){
- colorView.setChangeBackground(color);
- }
- }
5. ColorSelect.java : 색을 선택할 수 있게 지정해준다.(클릭)
- package com.example.rhkdg.myapplication;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- interface OnColorClickListener{
- public void onColorClick(int color);
- }
- public class ColorSelect extends Fragment implements View.OnClickListener{
- OnColorClickListener onColorClickListener;
- public void onAttach(Context context){
- super.onAttach(context);
- onColorClickListener = (OnColorClickListener) context;
- }
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
- View v = inflater.inflate(R.layout.color_select_layout, null);
- v.findViewById(R.id.v_red).setOnClickListener(this);
- v.findViewById(R.id.v_green).setOnClickListener(this);
- v.findViewById(R.id.v_blue).setOnClickListener(this);
- return v;
- }
- public void onClick(View v){
- int color = 0;
- switch(v.getId()){
- case R.id.v_red:
- color = Color.RED;
- break;
- case R.id.v_blue:
- color = Color.BLUE;
- break;
- case R.id.v_green:
- color = Color.GREEN;
- break;
- }
- onColorClickListener.onColorClick(color);
- }
- }
6. ColorView.java : ColorSelect에 의해서 선택된 색을 보여준다.
- package com.example.rhkdg.myapplication;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentPagerAdapter;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class ColorView extends Fragment{
- View v;
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
- v = inflater.inflate(R.layout.color_view_layout, null);
- return v;
- }
- public void setChangeBackground(int color){
- v.findViewById(R.id.v).setBackgroundColor(color);
- }
- }
▶ 실행결과
- 초기화면
- BLUE색상 클릭
- GREEN색상 클릭
- RED색상 클릭
댓글 없음:
댓글 쓰기