Android:应用栏上的片段

时间:2017-03-04 14:45:54

标签: android layout fragment overlay

我有一个Navigation Drawer的应用和一个ScrollViewwidgets的片段。

问题是片段小部件出现在应用栏上。是否可以调整片段布局,使其从应用栏开始?

提前谢谢!

截图: enter image description here

这是我的片段xml

<RelativeLayout 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="it.bitrack.fabio.bitrack.AssetView"
    android:id="@+id/assetView_relative_layout">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:text="17AB05"
                android:ems="10"
                android:id="@+id/assetCode" />

            <Button
                android:text="Fetch data"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/fetchUpdateButton" />

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/lineChart"
                android:layout_width="match_parent"
                android:layout_height="250dp" />

            <TextView
                android:layout_width="match_parent"
                android:id="@+id/selectionTextView"
                android:layout_height="wrap_content"
                android:text="Nothing selected" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

这是我的活动主xml

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

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

    <RelativeLayout 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="it.bitrack.fabio.bitrack.AssetView"
        android:id="@+id/container">
    </RelativeLayout>

    <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:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

这是我的应用栏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="it.bitrack.fabio.bitrack.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="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

    <android.support.design.widget.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" />

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

2 个答案:

答案 0 :(得分:0)

所以我假设您要对片段进行充气,请将层次结构用作:

activity_main.xml中
的FrameLayout
--DrawerLayout
----包括 app_bar_main
---- NavigationView

app_bar_main.xml:
CoordinatorLayout
--include content_main

content_main.xml:
RelativeLayout的
--FrameLayout (fragment_container)

答案 1 :(得分:0)

<强> content_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/drawer_app_bar_dashboard"><FrameLayout
    android:id="@+id/texeting_fram"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" /></LinearLayout>

您的java文件

setContentView(R.layout.activity_drawer_dashboard);//get tool bar for drawer navigation and hide main action bar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//initial fragment
    fragment = new Fragment_Main_Dashboard();
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.texeting_fram, fragment);
    fragmentTransaction.commit();