프로그래밍/안드로이드

안드로이드 - DialogFragment에 스타일과 테마 지정하기

가카리 2015. 10. 24. 23:04
반응형

 

일반 대화상자에 비해 대화상자 프래그먼트는 다양한 스타일과 테마를 쉽게 적용할 수 있다는 이점이 있다.

 

프래그먼트는 다음 메소드만 호출하면 된다. 단 스타일과 테마는 대화상자를 생성할 때 적용 되므로 생성하기 전에 이 메소드를 호출해야한다.

 

    void setStyle(int style, int theme)

 

다음 예제는 스타일과 테마의 조합 몇 가지를 보여준다.

 

그리고 예제는 3개의 파일로 구성된다.

 

dialog_style_theme.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

<TextView

    android:id="@+id/result"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:textSize="16sp"

    android:text=""

    />

<Button

    android:id="@+id/btn1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="Normal"

    />

<Button

    android:id="@+id/btn2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="No Title"

    />

<Button

    android:id="@+id/btn3"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="No Frame"

    />

<Button

    android:id="@+id/btn4"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="No Input"

    />

<Button

    android:id="@+id/btn5"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="Holo"

    />

<Button

    android:id="@+id/btn6"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="Holo Light Dialog"

    />

<Button

    android:id="@+id/btn7"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="Holo Light"

    />

<Button

    android:id="@+id/btn8"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="mOnClick"

    android:text="Holo Light Panel"

    />

</LinearLayout>

 

namegenderfragment.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    >

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    >

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="이름"

    />

<EditText

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="김아무개"

    />

</LinearLayout>

<RadioGroup

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:checkedButton="@+id/man"

    >

<RadioButton

    android:id="@id/man"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="남자"

    />

<RadioButton

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="여자"

    />

</RadioGroup>

</LinearLayout>

 

DialogStyleTheme.java

 

package com.example.ch20_dialogstyletheme;

 

import android.app.Activity;

import android.app.DialogFragment;

import android.app.Fragment;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

 

public class DialogStyleTheme extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_style_theme);

    }

 

    public void mOnClick(View v){

        switch(v.getId()){

        case R.id.btn1:

            showDialog(DialogFragment.STYLE_NORMAL, 0);

            break;

        case R.id.btn2:

            showDialog(DialogFragment.STYLE_NO_TITLE, 0);

            break;

        case R.id.btn3:

            showDialog(DialogFragment.STYLE_NO_FRAME, 0);

            break;

        case R.id.btn4:

            showDialog(DialogFragment.STYLE_NO_INPUT, 0);

            break;

        case R.id.btn5:

            showDialog(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo);

            break;

        case R.id.btn6:

            showDialog(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_Dialog);

            break;

        case R.id.btn7:

            showDialog(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light);

            break;

        case R.id.btn8:

            showDialog(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_Panel);

            break;

        }

    }

    

    void showDialog(int style, int theme){

        FragmentManager fm = getFragmentManager();//프래그먼트 매니저를 가져옴

        FragmentTransaction tr = fm.beginTransaction();//

        Fragment prev = fm.findFragmentByTag("dialog");//dialog 프래그먼트를 찾음

        if(prev != null){

            tr.remove(prev);

        }

        tr.addToBackStack(null);//백버튼을 누르면 이전 프래그먼트를 복원함

        NameGenderFragment newFragment = NameGenderFragment.newInstance(style, theme);

        newFragment.show(tr, "dialog");//프래그먼트 내부의 대화상자를 화면으로 출력함

    }

    

    public static class NameGenderFragment extends DialogFragment{

        static NameGenderFragment newInstance(int style, int theme){

            NameGenderFragment df = new NameGenderFragment();

            

            Bundle args = new Bundle();//인수전달을 위한 Bundle

            args.putInt("style", style);

            args.putInt("theme", theme);

            df.setArguments(args);

            

            return df;

        }

        

        //생명주기 onAttach 다음 여기로 ui작업은 불가능

        public void onCreate(Bundle savedInstanceState){

            super.onCreate(savedInstanceState);

            

            int style = getArguments().getInt("style");

            int theme = getArguments().getInt("theme");

            

            setStyle(style, theme);

        }

        

        //onCreate 다음 여기로옴 Layout Inflater 하는 부분

        public View onCreateView(LayoutInflater inflater, ViewGroup container,

                Bundle savedInstanceState){

            View root = inflater.inflate(R.layout.namegenderfragment, container, false);

            return root;

        }

    }

    

}

 

실행 화면

 

 

반응형