【问题标题】:Fragment overlay ActionBar and missing content in FragmentFragment 覆盖 ActionBar 和 Fragment 中缺少的内容
【发布时间】:2017-05-29 08:30:11
【问题描述】:

我无法解决这个问题。

这里是我的Main activity

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, AttendanceFragment.OnFragmentInteractionListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);

    setSupportActionBar(toolbar);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.nav_drawer_open, R.string.nav_drawer_close);

    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView.setNavigationItemSelectedListener(this);

    showAttendanceFragment();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.nav_drawer, menu);
    return true;
}
private void showAttendanceFragment() {
    AttendanceFragment fragment = new AttendanceFragment();
    FragmentManager fragmentManager = this.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container,fragment);
    fragmentTransaction.commit();
}


@Override
public void onFragmentInteraction(Uri uri) {

}

}

这里是main xlm

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:background="@color/primary">

    <include
        layout="@layout/app_bar_main"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:itemTextColor="@color/accent"
        app:itemIconTint="@color/accent"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        android:background="@color/primary"/>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"/>

</android.support.v4.widget.DrawerLayout>

bar

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <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="@color/primary_darker"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

还有fragment

<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="com.example.hoangdang.diemdanh.Fragments.AttendanceFragment">

    <TextView
        android:id="@+id/id1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
    <TextView
        android:id="@+id/id2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
</FrameLayout>

我尝试使用linear layout,但不起作用

【问题讨论】:

    标签: android android-fragments android-actionbar


    【解决方案1】:

    您应该了解DrawerLayout。你可以在这里查看:Google DrawerLayout Guide。正如谷歌指南的注释:

    主内容视图(上面的 FrameLayout)必须是 DrawerLayout 中的第一个子视图,因为 XML 顺序意味着 z 顺序,并且抽屉必须位于内容的顶部。

    主内容视图设置为匹配父视图的宽度和高度,因为它代表了隐藏导航抽屉时的整个 UI。

    抽屉视图(ListView)必须使用 android:layout_gravity 属性指定其水平重力。要支持从右到左 (RTL) 语言,请使用“start”而不是“left”指定值(因此当布局为 RTL 时,抽屉会显示在右侧)。

    抽屉视图以 dp 为单位指定其宽度,高度与父视图匹配。抽屉宽度不应超过 320dp,以便用户始终可以看到主要内容的一部分。

    正如我们在您的xml 中看到的:

    1. 您的主要内容视图是AppBar,这不好。
    2. AppBarFrameLayoutlayoutParams 中都有match_parent,这就是您遇到问题的原因。

    那么,如何解决呢?您必须仅将 AppBar 和 FrameLayout 组合在 1 个视图下(Relative 或 LinearLayout),并将其作为 DrawerLayout 的第一个子级

    【讨论】:

      【解决方案2】:

      1.activity_main.xml 中删除您的FrameLayout,并将其放到app_bar_main.xml 下的AppBarLayout

      2.FrameLayout 中删除attribute android:layout_marginTop="?attr/actionBarSize" 2.app:layout_behavior="@string/appbar_scrolling_view_behavior" 属性添加到FrameLayout

      更新您的activity_main.xml 如下:

      <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:openDrawer="start"
          android:background="@color/primary">
      
          <include
              layout="@layout/app_bar_main"
              android:layout_height="match_parent"
              android:layout_width="match_parent"/>
      
          <android.support.design.widget.NavigationView
              android:id="@+id/nav_view"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_gravity="start"
              android:fitsSystemWindows="true"
              app:itemTextColor="@color/accent"
              app:itemIconTint="@color/accent"
              app:headerLayout="@layout/nav_header_main"
              app:menu="@menu/activity_main_drawer"
              android:background="@color/primary"/>
      
      </android.support.v4.widget.DrawerLayout>
      

      更新app_bar_main.xml如下:

      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:fitsSystemWindows="true"
          tools:context=".MainActivity">
      
          <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="@color/primary_darker"
                  app:popupTheme="@style/AppTheme.PopupOverlay"/>
      
          </android.support.design.widget.AppBarLayout>
      
          <!-- Content :: Fragments-->
          <FrameLayout
              android:id="@+id/fragment_container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
      </android.support.design.widget.CoordinatorLayout>
      

      希望这会奏效~

      【讨论】:

      • 如果我的回答有助于解决您的问题,那么您应该将其标记为正确答案。
      • 我已经标记了其他答案,因为他不仅详细解释了代码
      【解决方案3】:

      添加布局行为并尝试...

      <FrameLayout
              android:id="@+id/fragment_container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
      

      【讨论】:

      • 感谢您的回复,但这只是解决方案的一部分,您可以查看@FAT 和 Kingfisher Phuoc 答案的解释和代码示例
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多