appbar定位不正确

时间:2018-04-21 06:40:33

标签: android android-appbarlayout

在我的协调器布局中,我添加了另一个布局,使AppBarLayout移动到顶部并位于状态栏下,换句话说,工具栏高度是它的实际高度的一半大小,为什么会发生这种情况?而且工具栏的标题也没有正确定位。

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ma_main">



<android.support.design.widget.AppBarLayout
    android:id="@+id/ma_appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
    android:id="@+id/ma_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/AppTheme.popup"/>
</android.support.design.widget.AppBarLayout>


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


<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ma_fab"
    app:layout_anchor="@id/ma_recycler_layout"
    app:layout_anchorGravity="bottom|end"
    android:layout_margin="15dp"
    app:fabSize="normal"
    android:src="@drawable/pencil"
    android:elevation="8dp" />
   </android.support.design.widget.CoordinatorLayout>

并包含布局

<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:id="@+id/ma_recycler_layout">



<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ma_recyclerView"
    android:visibility="invisible">

</android.support.v7.widget.RecyclerView>

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="2dp"
    android:padding="5dp"
    android:id="@+id/ma_progressbar"
    android:visibility="visible"
    android:indeterminate="true"
    /></LinearLayout>

1 个答案:

答案 0 :(得分:0)

在xml中进行一些更改,如下所示..

<RelativeLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/ma_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <android.support.v7.widget.Toolbar
            android:id="@+id/ma_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/AppTheme.popup" />
    </android.support.design.widget.AppBarLayout>

</RelativeLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/ma_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:elevation="8dp"
    android:src="@drawable/pencil"
    app:fabSize="normal"
    app:layout_anchor="@id/ma_recycler_layout"
    app:layout_anchorGravity="bottom|end" />

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

然后在下面的recyler视图布局之后..

<RelativeLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/ma_recycler_layout">



<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ma_recyclerView"
    android:visibility="invisible">

</android.support.v7.widget.RecyclerView>

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:padding="5dp"
    android:id="@+id/ma_progressbar"
    android:visibility="visible"
    android:indeterminate="true"
    android:layout_centerInParent="true"
    /></RelativeLayout>