프로그래밍/안드로이드

안드로이드 - 프래그먼트 대화상자를 액티비티 안에 배치하기

가카리 2015. 10. 12. 22:51
반응형

 

전 예제 DialogFragment로 대화상자 만들기에 이어서 대화상자를 액티비티 안에 배치하기 예제입니다.

 

 

위와 같이 예제를 구성합니다.

 

dialogfragmentembed.xml

 

<LinearLayout 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:orientation="vertical">

 

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="대화상자 프래그먼트를 액티비티에 내장"/>

<fragment

android:name="com.example.dialogfragmentembed.DialogFragmentEmbed$NameGenderFragment"

android:id="@+id/namegenderfragment"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

 

</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>

 

 

DialogFragmentEmbed.java

 

package com.example.dialogfragmentembed;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.app.DialogFragment;

 

public class DialogFragmentEmbed extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialogfragmentembed);

    }

 

    public static class NameGenderFragment extends DialogFragment{

        @Override

        public View onCreateView(LayoutInflater inflater, ViewGroup container,

                Bundle savedInstanceState) {

            // TODO Auto-generated method stub

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

            return root;

        }

        

    }

}

 

 

실행 화면

 

 

반응형