activity如何冻结顶部的Toolbar?
How to freeze the Toolbar at the top of the activity?
我正在尝试将我的工具栏固定在顶部,就像正常情况一样 'Action Bar' 就像您单击 Twitter 时一样 post 无论您滚动多少,顶部的工具栏都会保持在那里下来。
我在 "how to make the Toolbar collapse and other cool effects" 上找到了很多指南,但我想从中得到的是充当操作栏并保持在顶部,有没有一种简单的方法可以做到这一点?不使用 (CoordinatorLayout, CollapsingToolbarLayout, ...) ?
是的,您可以将工具栏固定在 activity 的顶部,因为您必须像这样创建一个名为 toolbar_layout
的布局文件
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@color/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:theme="@style/ToolbarStyle"
app:titleTextColor="@color/colorWhite"
app:subtitleTextColor="@color/colorWhite"
app:titleTextAppearance="@style/ToolbarStyle"/>
并且在 activity 布局中,即 activity_main
文件中,像这样包含它:-
<LinearLayout
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:orientation="vertical">
<include layout="@layout/toolbar_layout"/>
</LinearLayout>
然后相应地设计布局
干杯快乐编码。
您可以这样布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Your content here -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
或者如果您有列表
,则用 RecyclerView
代替 ScrollView + LinearLayout
我正在尝试将我的工具栏固定在顶部,就像正常情况一样 'Action Bar' 就像您单击 Twitter 时一样 post 无论您滚动多少,顶部的工具栏都会保持在那里下来。
我在 "how to make the Toolbar collapse and other cool effects" 上找到了很多指南,但我想从中得到的是充当操作栏并保持在顶部,有没有一种简单的方法可以做到这一点?不使用 (CoordinatorLayout, CollapsingToolbarLayout, ...) ?
是的,您可以将工具栏固定在 activity 的顶部,因为您必须像这样创建一个名为 toolbar_layout
的布局文件
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@color/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:theme="@style/ToolbarStyle"
app:titleTextColor="@color/colorWhite"
app:subtitleTextColor="@color/colorWhite"
app:titleTextAppearance="@style/ToolbarStyle"/>
并且在 activity 布局中,即 activity_main
文件中,像这样包含它:-
<LinearLayout
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:orientation="vertical">
<include layout="@layout/toolbar_layout"/>
</LinearLayout>
然后相应地设计布局
干杯快乐编码。
您可以这样布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Your content here -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
或者如果您有列表
,则用RecyclerView
代替 ScrollView + LinearLayout