【问题标题】:Coordinator layout, activity below toolbar协调器布局,工具栏下方的活动
【发布时间】:2016-01-12 21:29:45
【问题描述】:

我有一个带有协调器布局的应用程序,它在协调器布局中使用滑动选项卡和视图寻呼机。我正在尝试在工具栏下方放置一个活动(列表)。我尝试了许多不同的方法,但列表总是与工具栏重叠。

布局

<?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=".MainActivity">

    <LinearLayout 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="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            android:padding="2dp"
            app:titleMarginStart="20dp"
            android:theme="@style/AppTheme.AppBarOverlay"/>

        <com.passwordstore.utility.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary" />

        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@android:color/white"
            android:foreground="@drawable/shadow"/>

       <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v4.view.ViewPager>

    </LinearLayout>

    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/frameLayout">

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

    </FrameLayout>

    <android.support.design.widget.FloatingActionButton android:id="@+id/fabNew"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_add_white_24dp"
        app:layout_anchor="@+id/pager"
        app:layout_anchorGravity="bottom|right|end"/>

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

【问题讨论】:

    标签: android android-coordinatorlayout


    【解决方案1】:

    只需将app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到需要在工具栏下的布局中

    【讨论】:

      【解决方案2】:

      请试试这个:

      <?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=".MainActivity">
      
           <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fitsSystemWindows="true">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="?attr/colorPrimary"
                  android:fitsSystemWindows="true"
                  android:minHeight="?attr/actionBarSize"
                  android:padding="2dp"
                  app:titleMarginStart="20dp"
                  android:theme="@style/AppTheme.AppBarOverlay"/>
      
              <com.passwordstore.utility.SlidingTabLayout
                  android:id="@+id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@color/colorPrimary" />
      
              <View
                  android:layout_width="match_parent"
                  android:layout_height="4dp"
                  android:background="@android:color/white"
                  android:foreground="@drawable/shadow"/>
      
             <android.support.v4.view.ViewPager
                  android:id="@+id/pager"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1">
              </android.support.v4.view.ViewPager>
      
          </android.support.design.widget.AppBarLayout>
      
          <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
          android:layout_below= "@id/appbar"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/frameLayout">
      
              <include layout="@layout/activity_password_list" />
      
          </FrameLayout>
      
          <android.support.design.widget.FloatingActionButton android:id="@+id/fabNew"
              android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_add_white_24dp"
              app:layout_anchor="@+id/pager"
              app:layout_anchorGravity="bottom|right|end"/>
      
      </android.support.design.widget.CoordinatorLayout>
      

      我删除了 linearLayout 并将其替换为 appBarLayout ,在您的 Framelayout 中添加了 android:layout_below p>

      祝你好运

      【讨论】:

      • 我无法使用 appbarlayout,因为我的列表不随 viewpager 滚动,所以必须更改它。
      • 好的,保留您的 Linearlayout,但只需在 FrameLayout 中添加 android:layout_below= "@id/yourLinearLayoutID" 看看它是否解决了它
      • 你不能在 CoordinatorLayout 中使用 layout_below
      • 它不工作。底视图未显示。请提出相同的建议
      【解决方案3】:

      您可以将 CoordinatorLayout 替换为 LinearLayout(使用 android:orientation="vertical")。

      <RelativeLayout>
          <LinearLayout> (replaces the coordinator layout>
              <AppBarLayout/>
              <FrameLayout/>
          </LinearLayout>
          <FloatingActionButton/>
      </RelativeLayout>
      

      【讨论】:

      • 那我的浮动操作按钮有问题
      • 把这个新的LinearLayout放在RelativeLayout中。以及与LinearLayout同级的FAB(RelativeLayout的直接子级)
      猜你喜欢
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 2016-07-26
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多