Ale*_*scu 8 android android-coordinatorlayout
在CoordinatorLayout 的示例中,它始终放在Activity中,而Fragments都是滚动的.
可以合理地设想一个场景,虽然加载的某些片段是可滚动的而某些片段不是,但它们会被加载到活动中的同一个占位符中.在这种情况下,CoordinatorLayout应该放在Activity还是Fragments中?
一方面,因为您将工具栏指定为它的子项,所以它似乎都应该在Activity中.
另一方面,如果你在同一个占位符中加载了滚动和非滚动片段,那么这似乎很难配置(例如你为Fragment占位符指定了什么滚动行为?),所以它似乎应该存在碎片.但是,在这种情况下,每个片段是否必须再次设置工具栏?
Q:CoordinatorLayout应该放在Activity中还是Fragments中?
A:您可以在不同情况下在activity或fragment中使用CoordinatorLayout
Q:一方面,因为你指定了Toolbar作为它的子项,看起来它应该全部在Activity中。
您可以将工具栏添加到片段布局而不是活动布局,如下所示
在此示例中,我使用 NavigationView 和 viewPager 创建活动:
所以为我的活动创建布局(此布局用于MainActivity):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"
app:itemIconTint="@color/tint_item_color"
app:itemTextColor="@color/tint_item_color"
/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
为了使用 tablayout 创建 viewPager 我创建了这个布局(这个布局适用于fragmentViewPager):
<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:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/toolbar_main"
android:id="@+id/toolbar"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="@color/tab_color"
app:tabSelectedTextColor="@color/tab_color"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
然后使用我在 viewPager 中使用的 listFragment 的另一个布局:
<android.support.design.widget.CoordinatorLayout
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"
android:id="@+id/main_list_content"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@color/background_window">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="0dp"
android:id="@+id/exersice_listviwe"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginBottom="64dp"
android:clickable="true"
android:src="@drawable/ic_done"
fab:layout_anchor="@+id/main_list_content"
fab:layout_anchorGravity="bottom|right"
/>
</android.support.design.widget.CoordinatorLayout >
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1081 次 |
| 最近记录: |