首页 文章

膨胀的碎片内容忽略工具栏[重复]

提问于
浏览
0

这个问题在这里已有答案:

我是android studio的新手,所以我想为这些基本问题道歉 .

我试图用tablayout膨胀一个片段 . 可悲的是,我的布局文件忽略了工具栏,我不知道为什么 . 也许你可以给我一个提示:

要膨胀的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".Drawer_Emergencies"
    >

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:icon="@drawable/logo_suggestion"
            android:text="tab1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <com.google.android.material.tabs.TabItem
            android:icon="@drawable/logo_suggestion"
            android:text="tab2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
        <com.google.android.material.tabs.TabItem
            android:icon="@drawable/logo_suggestion"
            android:text="tab3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

    </com.google.android.material.tabs.TabLayout>


</FrameLayout>

而预览看起来像这样:

这个xml被这个方法膨胀:

public class Drawer_Emergencies extends Fragment{


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);


        return inflater.inflate(R.layout.fragment_drawer__emergencies, container, false);

    }
}

我正在使用导航工具来设置此XML文件中的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/content_main_frame"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

而content_main是一个如下所示的framelayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/content_main_frame"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

How I change my Fragment:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment newFragment;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (id == ..) {
        } else if (id == R.id.nav_emergencies) {
            transaction.replace(R.id.content_main_frame, new Drawer_Emergencies());
        transaction.addToBackStack(null);
        transaction.commit();

}}

end result 现在看起来像这样,而我的 fragment Drawer_Emergencies 在正确的点上膨胀,但 R.id.content_main_frame 仍然是存在的并且没有被替换:

2 回答

  • 0

    如果要将协调器布局用作主要布局,则必须将此参数用于内容区域

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
    <Your Content Layout
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
             <!-- Your scrolling content -->
    
         </Your Content Layout>
    

    支持库包含一个特殊的字符串资源 @string/appbar_scrolling_view_behavior ,它映射到AppBarLayout.ScrollingViewBehavior,用于在此特定视图上发生滚动事件时通知AppBarLayout . 必须在触发事件的视图上 Build 行为 .

    希望这会有所帮助......

  • 0

    Solution:CoordinatorLayout 里面使用 LinearLayout ,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:id="@+id/app_bar_main">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">
    
                <androidx.appcompat.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:fitsSystemWindows="true"/>
    
            </com.google.android.material.appbar.AppBarLayout>
    
    
            <!--<include layout="@layout/content_main" />-->
    
        </LinearLayout>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    试试吧,希望它有效 .

相关问题