Invoke another layout view from a fragment

Asked 2 years ago, Updated 2 years ago, 92 views

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>

I'd like to use the tool bar declared here in the fragment.

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);
 ...
}

Is this the code to find the tool bar declared in the activity main? It doesn't apply when I use it like this.

 @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();
    ...
    }

android fragment

2022-09-22 14:20

1 Answers

Activity_main is inflated when an activity is created, so it is meaningless to inflate the fragment like the code you wrote. Please modify the code as follows.

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1, container, false);
    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);    
    setHasOptionsMenu(true);

    ...
    return view;
}

I think it would be better to call setSupportActionBar (toolbar) from the activity.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ...
}


2022-09-22 14:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.