java - Fragment 布局与 Activity Titlebar 重叠

标签 java android android-layout android-fragments

我有一个 Activity ,即 Material Design Drawer,以及当用户单击抽屉选项时从 Activity java 文件中调用的一些 fragment 。 问题在于 fragment 布局与 Activity 标题栏重叠,匹配所有屏幕,而不是位于 Activity 内部。 在 AndroidStudio 预览中, fragment 内容设置正确: enter image description here

但是当我将 apk 加载到我的手机中时,我看到如下内容: enter image description here

这是我的源代码: - fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <EditText
        android:id="@+id/subjectSelector"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="Write subject initials"/>

    <Button
        android:id="@+id/queryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        style="@style/Base.Widget.AppCompat.Button.Borderless"
        android:text="Search"
        android:layout_alignBottom="@+id/subjectSelector"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/subjectSelector"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/subjectName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/subjectTeachers"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/subjectDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/subjectCredits"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

抽屉.java

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment newFragment;
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (id == R.id.nav_timetable) {
        newFragment = new TimetableMainMenu();
    } else if (id == R.id.nav_notifications) {
        newFragment = new NotificationsMainMenu();
    } else if (id == R.id.nav_schedule) {
        newFragment = new ScheduleMainMenu();
    } else if (id == R.id.nav_class_availability) {
        newFragment = new ClassAvailabilityMainMenu();
    } else if (id == R.id.nav_subject_info) {
        newFragment = new SubjectInfoMainMenu();
    } else newFragment = null;

    if (newFragment != null) {
        transaction.replace(R.id.main_menu_fragment_container, newFragment);
        transaction.addToBackStack(null);
    } else {
        Toast.makeText(MainMenuActivity.this, "Not implemented yet", Toast.LENGTH_SHORT).show();
    }
    transaction.commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
  • MainMenu.xml(抱歉,我无法将其更新为代码格式)

enter image description here

  • app_bar_main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:fitsSystemWindows="true"
    tools:context="com.upc.fib.racopocket.MainMenuActivity">

    <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>

    <include layout="@layout/content_main_menu" />



</android.support.design.widget.CoordinatorLayout>
  • content_main_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_menu_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

我该如何解决这个问题?

最佳答案

我得到了答案,这只是一件愚蠢的事情。在 content_main_menu.xml 上,框架布局与所有屏幕匹配,所以我只需要留下 actionBarSize 的 marginTop。这是新的 content_main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_menu_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

关于java - Fragment 布局与 Activity Titlebar 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37972066/

相关文章:

javascript - 对具有 "A/B"格式的数字的数组进行排序?

android - 将资源(drawable)组织成Android Studio项目的一些疑惑

android - 获取 RelativeLayout 尺寸

Android:如何在不调整大小的情况下推送整个 Activity ,显示软键盘时平移

android - LinearSnapHelper 不会捕捉到 RecyclerView 的边缘项目

java - 如何通过另一个面板更改CardLayout的一个面板

java - 使用@Singleton 和@Stateless 加载和缓存应用程序范围的数据

android - 如何在android中创建共享 View

java - 60 秒后停止异步任务

android - 以编程方式在整个 Android 应用程序中禁用屏幕旋转