使用新的设计库有几个新的布局,如果开发人员愿意,可以改变工具栏的行为方式.由于不同的片段具有不同的行为和目标,例如,具有折叠工具栏的画廊片段显示重要照片,或者没有滚动视图的片段,不需要appbarlayout来隐藏工具栏,在活动中具有单个工具栏可以证明很难.
那么,我应该将工具栏移动到每个片段吗?如果是这样,我每次显示片段时都必须设置supportActionBar,并且还要在片段中引用活动,这会使片段的独立性无效.如果我单独将Activity放在Activity中,我必须为每个片段中的每种行为定义多个布局.什么是最好的方法?
android android-fragments android-activity android-actionbar
我有一个带有不同碎片的导航抽屉.每个Fragment应该使用的默认工具栏,除了Fragment需要折叠的工具栏Toolbar.
如何在片段的工具栏之间切换?
我创建了一个具有视差效果的布局,我在片段中使用它.
<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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/collapsingImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/baner_1"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!--OTHER SCROLABLE STUFF-->
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
只要片段在Activity的根目录中膨胀(进入container_main),它就可以正常工作.
所以Activity的布局如下所示:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
现在我想在我的Activity中添加一个DrawerLayout,这就是我卡住的地方.
我通过添加DrawerLayout更改了Activity的布局.
然后将上面提到的片段膨胀成相同的container_main,但是既没有视差也没有NestedScrollView工作.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>
<ListView
android:id="@+id/drawerListView"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
使用DrawerLayout和使用折叠工具栏的诀窍在哪里?
android android-layout drawerlayout android-toolbar android-collapsingtoolbarlayout