【问题标题】:CoordinatorLayout Makes views UnpositionedCoordinatorLayout 使视图未定位
【发布时间】:2019-04-03 09:52:38
【问题描述】:

对不起,我的英语不好。 我试图使用 Coordinatorlayout 使用 recyclerview 制作滚动工具栏。滚动工作正常,但视图位于工具栏下方,如 中的滑动刷新布局显示。 Activity_main.xml 我在 FrameLayout mobile_container

中实现了一个片段
<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:id="@+id/main_content"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.naveed.youtubepro.activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="@string/app_name">
        </android.support.v7.widget.Toolbar>

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

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

        <FrameLayout
            android:id="@+id/mobile_container"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>
        <me.majiajie.pagerbottomtabstrip.PageNavigationView
            android:id="@+id/navigation"
            android:elevation="8dp"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:background="#FFF"
            app:menu="@menu/bottom_navigation_items"/>

    </LinearLayout>

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

片段布局

<android.support.v4.widget.SwipeRefreshLayout
    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:id="@+id/mainandroid.support.v4.widget.SwipeRefreshLayout">
    <com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyclerViee"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>

【问题讨论】:

  • 在 LinearLayout 中添加所有视图和 ViewsGroup 并使其方向垂直,如@Kilran.Thanks 的回答中所述......
  • 兄弟它已经在那里了
  • 使宽度“match_parent”
  • 哪里需要 match_parent

标签: android android-coordinatorlayout


【解决方案1】:

这样做。希望这能奏效。

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

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/txt_forget_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:title="@string/app_name">
            </android.support.v7.widget.Toolbar>

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

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

            <FrameLayout
                android:id="@+id/mobile_container"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="0dp">

            </FrameLayout>
            <me.majiajie.pagerbottomtabstrip.PageNavigationView
                android:id="@+id/navigation"
                android:elevation="8dp"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:background="#FFF"
                app:menu="@menu/bottom_navigation_items"/>

        </LinearLayout>

    </LinearLayout>



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

【讨论】:

  • 这有效,但工具栏没有使用 recyclerview 滚动
  • @Brown 也许你知道“工具栏应该固定在活动中”。
【解决方案2】:

您的 LinearLayout 将回收器视图保存为 android:layout_height="match_parent",因此它将占据屏幕的所有高度,因为父级是根布局。

你必须让这个 LinearLayout 取所有高度减去标题栏高度

您可以轻松做到这一点:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1">
    <!-- frame and bottom navigation -->
</LinearLayout>

============== 更新==============

其实你甚至不需要这个 LinearLayout 试试这个(按照 Rahul Kushwaha 的建议,将整个东西包装在 LinearLayout 中,但你不需要内部 LinearLayout):

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

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/txt_forget_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="@string/app_name">
        </android.support.v7.widget.Toolbar>

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

    <FrameLayout
        android:id="@+id/mobile_container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </FrameLayout>
    <me.majiajie.pagerbottomtabstrip.PageNavigationView
        android:id="@+id/navigation"
        android:elevation="8dp"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="#FFF"
        app:menu="@menu/bottom_navigation_items"/>

</LinearLayout>

【讨论】:

  • 布局内的视图现在完全不可见
【解决方案3】:

设置您的 LinearLayout 属性 android:layout_marginTop="?attr/actionBarSize"

【讨论】:

  • 我已经尝试过了,但是当工具栏滚动时,工具栏和其他布局之间会出现空白
猜你喜欢
  • 2017-12-02
  • 1970-01-01
  • 2019-09-17
  • 2016-06-21
  • 2018-05-06
  • 1970-01-01
  • 2018-03-06
  • 2016-07-02
相关资源
最近更新 更多