편집 기록

편집 기록
  • 프로필 정토드님의 편집
    날짜2017.01.03

    프레그먼트에서 다른 레이아웃 뷰 호출


    activity_main.xml

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.app.FragmentTabHost
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabhost">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TabWidget
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/tabs"></TabWidget>
            </LinearLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="49dp"
                android:id="@+id/tabcontent"/>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/realtabcontent"
                android:layout_weight="1"/>
    
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
    

    여기에 선언된 툴바를 fagment에서 사용하고 싶습니다.

    fragment1.java

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInistanceState) {
            // Inflate the layout for this fragment0
            View view = inflater.inflate(R.layout.fragment1, container, false);
            View v = inflater.inflate(R.layout.activity_main, container, false);
    
            Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
            ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
            setHasOptionsMenu(true);
     ...
    }
    
    

    이 부분에서 엑티비티 메인에 선언된 툴바를 찾아오는 코드가 맞나요?? 이렇게 사용하니 적용이 안되네요 ..

     @Override
        public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu_main, menu);
    
            final MenuItem item = menu.findItem(R.id.action_search);
    
            SearchView searchView = (SearchView) item.getActionView();
        ...
        }
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2016.10.24

    프레그먼트에서 다른 레이아웃 뷰 호출


    activity_main.xml

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.app.FragmentTabHost
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabhost">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TabWidget
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/tabs"></TabWidget>
            </LinearLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="49dp"
                android:id="@+id/tabcontent"/>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/realtabcontent"
                android:layout_weight="1"/>
    
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
    

    여기에 선언된 툴바를 fagment에서 사용하고 싶습니다.

    fragment1.java

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInistanceState) {
            // Inflate the layout for this fragment0
            View view = inflater.inflate(R.layout.fragment1, container, false);
            View v = inflater.inflate(R.layout.activity_main, container, false);
    
            Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
            ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
            setHasOptionsMenu(true);
     ...
    }
    
    

    이 부분에서 엑티비티 메인에 선언된 툴바를 찾아오는 코드가 맞나요?? 이렇게 사용하니 적용이 안되네요 ..

     @Override
        public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu_main, menu);
    
            final MenuItem item = menu.findItem(R.id.action_search);
    
            SearchView searchView = (SearchView) item.getActionView();
        ...
        }