마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안녕하세요 프래그먼트 질문드릴게요

0 추천
-----------------------------------MainFragment----------------------------------------------------------
package org.androidtown.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class MainFragment extends android.support.v4.app.Fragment {
    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_main,container,false);

        Button button  = (Button) rootView.findViewById(R.id.main_button);
        button.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View view) {
                  MainActivity activity = (MainActivity)getActivity();
                  activity.onFragmentChanged(0);
              }
          }
        );
        return rootView;
    }
}
---------------------------------------------------------------------------------------------------------
-------------------------------------------MenuFragment------------------------------------------
package org.androidtown.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class MainFragment extends android.support.v4.app.Fragment {
    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_main,container,false);

        Button button  = (Button) rootView.findViewById(R.id.main_button);
        button.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View view) {
                  MainActivity activity = (MainActivity)getActivity();
                  activity.onFragmentChanged(0);
              }
          }
        );
        return rootView;
    }
}

 

안녕하세요 제가

activity_main.xml 에 프래그먼트를 태그로 추가하였습니다

이 xml 에는 텍스트뷰 하나와 프래그먼트만 있습니다

그리고 프래그먼트태그에 name 속성으로 처음 시작할때부터 메인 화면에

프래그먼트가 나타나게 해 두었습니다. 그리고 그 나타난 프래그먼트에 속해있는 버튼을 눌러서 또다른

프래그먼트로 이동했다가 다시 또 그 프래그먼트에서  처음 프래그먼트로 돌아올수 있게 해놓았습니다

 

그런데 여기서 의문점이 처음에 실행할때는 activity_main.xml 의 텍스트뷰가 프래그먼트에 의해

가려지지 않는다는것입니다. layout 가로세로 모두 match parent입니다

그런데 프래그먼트 안에서 버튼을 눌러 다른 프래그먼트를 부를때는 그 프래그먼트에의해 activity_main.xml

의 텍스트뷰가 가려집니다 아래가 제 코드입니다. 그냥 xml에서 태그로 추가하는건 통과되서 다 보이고

메인엑티비티에서 코드로 추가하는건 그 위에 띄워져서 다 가려진다 이렇게 받아들이면 되는건가요?

-------------------------------[activity_main.xml]--------------------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">


    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:name="org.androidtown.fragment.MainFragment"/>



    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
      />

</RelativeLayout>
---------------------------------------------------------------------------------
------------------------[fragment_main.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:background="@android:color/holo_blue_dark"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="메인 프래그먼트"
        android:textSize="30dp" />

    <Button
        android:id="@+id/main_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="30dp"
        android:text="메뉴 화면으로" />
</LinearLayout>

---------------------------------------------------------------------------------

-------------------------[fragment_menu.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:background="@android:color/holo_orange_dark"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="메뉴 프래그먼트"
        android:textSize="30dp" />

    <Button
        android:id="@+id/menu_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="메안 화면으로" />
</LinearLayout>

---------------------------------------------------------------------------------
--------------------------------[MainActivity]-------------------------------
package org.androidtown.fragment;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    MainFragment mainFragment;
    MenuFragment menuFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainFragment = new MainFragment();
        menuFragment = new MenuFragment();


    }
    public void onFragmentChanged(int num){
        if(num == 0){
            getSupportFragmentManager().beginTransaction().replace(R.id.container,menuFragment).commit();
        }else if (num == 1){
            getSupportFragmentManager().beginTransaction().replace(R.id.container,mainFragment).commit();
        }


    }
}
----------------------------------------------------------------------------------------

 

gerghthrt (120 포인트) 님이 2017년 8월 19일 질문
...