简体   繁体   English

片段布局重叠活动标题栏

[英]Fragment layout overlaps Activity Titlebar

I have an activity, that is a Material Design Drawer, and some fragments that are called from the activity java file when an user clicks on an option of the drawer. 我有一个活动,即Material Design Drawer,以及当用户点击抽屉选项时从活动java文件中调用的一些片段。 The problem is that the fragment layout, overlaps the activity titlebar, matching all the screen, instead of being positioned inside the activity. 问题是片段布局与活动标题栏重叠,匹配所有屏幕,而不是位于活动内部。 In the AndroidStudio preview, fragment content is set correctly: 在AndroidStudio预览中,片段内容设置正确: 在此输入图像描述

But when I load the apk into my phone, I see something like this: 但是当我将apk加载到手机中时,我看到的是这样的: 在此输入图像描述

Here is my source code: - fragment_layout.xml 这是我的源代码: - 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>

Drawer.java Drawer.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 (Sorry, I can't update this as code format) MainMenu.xml(对不起,我无法将其更新为代码格式)

在此输入图像描述

  • app_bar_main_menu.xml 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 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" /> 

How can I fix this? 我怎样才能解决这个问题?

I got the answer, it was just a stupid thing. 我得到了答案,这只是一个愚蠢的事情。 On the content_main_menu.xml, the frame layout is matching all the screen, so I just had to left a marginTop of actionBarSize. 在content_main_menu.xml上,框架布局匹配所有屏幕,所以我只需要保留marginTop of actionBarSize。 This is the new content_main_menu.xml: 这是新的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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM