2014-09-12 2 views
1

지금 Android 앱을 만들고 있는데 셸에 조각이 들어 있습니다. 지금까지는 모든 것이 잘 작동하지만, 나는 완전히 이해할 수없는 질문에 왔습니다.Android Fragments : 프레임 또는 다른 레이아웃 내의 조각?

전체 화면 (FrameLayout - 기본 프레임으로 FrameLayout을 사용해야합니까?) 인 Frame이 있으며이 프레임 내부에는 사용자 상호 작용에 따라 변경되는 부분이 있습니다. 이러한 조각은 .xml 파일의 FrameLayouts에 있습니다. FrameLayouts 또는 조각이 될 수 있는지 여부를 궁금해합니다 ... 나는 이상적인 조각 인 Google지도 frredge를 만들어서 생각하게 만들었습니다.

그럼 내 질문은 다음과 같습니다. 차이가 있거나 성능에 어떤 영향을 주나요? 아니면 용도가 무엇이든간에 간단하게 사용할 수 있습니까? 합니다 (FrameLayout이 내부 조각을 모두 넣을 수있는 FrameLayout이 내부)

메인 프레임 (activity_main.xml) :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    tools:ignore="MergeRootFrame" > 

    <FrameLayout 
     android:id = "@+id/mainFrame" 
     android:layout_width = "match_parent" 
     android:layout_height = "match_parent" 
     android:layout_marginBottom = "@dimen/bottom_Main_Tabs"> 
     </FrameLayout> 

    <LinearLayout 
     android:layout_width = "match_parent" 
     android:layout_height = "@dimen/bottom_Main_Tabs" 
     android:layout_gravity = "bottom" 
     android:background="@color/grey2" 
     > 

     <ImageButton 
      android:id = "@+id/bottomButton_home" 
      android:layout_height = "match_parent" 
      android:layout_width = "0dp" 
      android:layout_weight = "1.0" 
      android:layout_marginLeft = "2dp" 
      android:layout_marginRight = "2dp" 
      android:background = "@drawable/ic_home_white" 
      android:onClick = "openHome" 
      /> 

     [...] 

    </LinearLayout> 

    <RelativeLayout 
     android:id = "@+id/TopBar" 
     android:layout_width = "match_parent" 
     android:layout_height = "@dimen/top_Bar_Hight" 
     android:layout_gravity="top" 
     android:background="@color/grey_transparentBy50" 
     > 

     <ImageView 
      android:id= "@+id/TopBarLogo" 
      android:layout_width = "@dimen/top_Bar_Hight" 
      android:layout_height = "@dimen/top_Bar_Hight" 
      android:background="@drawable/ic_launcher" 
      /> 

     [...] 

    </RelativeLayout> 

</FrameLayout> 

한 조각 (

가 무슨 뜻인지 보여주기 위해 여기에 몇 가지 코드 샘플은 FrameLayout이 :)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/home_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/blue" 
    tools:context="com.domain.app.HomeFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:textSize="100sp" 
     android:text="Home" /> 

</FrameLayout> 

다른 Frament (단편)

<fragment 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="@+id/map" 
      tools:context="com.domain.app.MapsActivity" 
      android:name="com.google.android.gms.maps.MapFragment" 
      tools:layout = "@layout/activity_main" 
    /> 

GoogleMaps 구현에 문제가 너무 많지만 아직 도움을 요청할 시간이 부족합니다.

미리 감사드립니다.

+1

xml 파일 콘텐츠를 붙여 넣을 수 있습니까? – aldorain

+0

질문이 업데이트되었습니다. – JRsz

답변

2

당신이 GoogleMapFragment 같은 XML 또는 XML에 FrameLayout이 (또는있는 LinearLayout이나 RelativeLayout의 같은 다른 뷰 그룹)를 사용하여 조각을 부착하는 경우. 두 방법 모두 같습니다. 프로그래밍 방식으로 (예 : 자바에서) 또는 XML로 정의하여 텍스트 뷰를 만드는 것이 더 쉽습니다. 자바

사용하여 조각 :

FragmentManager fragmentManager = getFragmentManager() 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
ExampleFragment fragment = new ExampleFragment(); 
fragmentTransaction.add(R.id.fragment_container, fragment); 
fragmentTransaction.commit(); 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

XML

에서

여기 R.id.fragment_container은 프레임 레이아웃의 ID입니다.

XML에서 조각을 사용하여 :

당신의 조각이 <fragment/> XML 태그에 의해 우리가 프로그래밍 조각을 사용하는 다른 측면에서 정의됩니다 XML에서 조각의 경우
<fragment android:name="com.example.ExampleFragment" 
      android:id="@+id/fragment"    
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

, 당신의 FrameLayout이는 컨테이너로 작동합니다 너의 조각을 위해.

결론 : FrameLayout을 사용하면 계층 구조가 하나 증가하지만 성능에 큰 영향을주지는 않습니다.

참조 : http://developer.android.com/guide/components/fragments.html#Adding

안드로이드 다음 조각 클래스는 레이아웃에서 인스턴스화 할 수있는 지정의 이름 속성.시스템이이 활동 레이아웃을 만들면 레이아웃 에 지정된 각 단편을 인스턴스화하고 각각에 대해 onCreateView() 메소드를 호출하여 각 조각의 레이아웃을 검색합니다. 시스템은 요소 대신에 프래그먼트가 리턴 한 뷰를 직접 삽입합니다.

+0

오케이, 여러 번 답변을 읽었지만 완전히 따라 할 수 없습니다. 조금 더 자세하게 설명해 주시겠습니까? 나는 매우 안드로이드에 익숙하다. – JRsz

+1

@JRsz : FrameLayout과 조각을위한 xml을 사용하는 것의 차이점은 바로 당신의 질문인가? –

+0

@JRsz : 내 대답을 업데이트했습니다. 이제는 이해가 더 쉽다고 생각합니다. –

관련 문제