gpt4 book ai didi

android - 工具栏位于 Appbar 之上 - CoordinatorLayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:41 24 4
gpt4 key购买 nike

我一直在努力使用 AppBarLayout/Toolbar/CoordinatorView。希望工具栏 在滚动时向上滑动,并在向下滚动时立即返回(与大多数阅读应用程序一样,g+ 也是一个例子)。然而,这就是我得到的:

正常:

Normal

一个小卷轴:

A little scroll

全屏滚动:

Everything scrolled

这是我的代码:

<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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="main.MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/id_appbar"
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/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:title="Hello World"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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


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

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

main__activitycontent:

<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="main.MainActivity"
tools:showIn="@layout/main__activity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text"/>

</android.support.v4.widget.NestedScrollView>

我已经阅读了很多博客文章,但他们的配置似乎都不适合我。向上滚动时隐藏/向下滚动时显示效果很好,只有 Toolbar 在 appbar 上移动。检查 Android View Hierarchy,我看到它们 Toolbar 位于深蓝色应用栏上方,并且它们一起向上移动。

编辑 1

CoordinatorLayout 中删除 fitsSytemWindows=true 会导致预期的行为,但是无论 CoordinatorLayout :

enter image description here

我的猜测是行为实际上没有改变,发生的是它不再覆盖应用栏,所以工具栏不会越过它。

编辑 2

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

编辑 3

package main;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.ee.oef.refactor.R;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main__activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.main__toolbar);
setSupportActionBar(toolbar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

最佳答案

问题出在:

app:layout_scrollFlags="scroll|enterAlwaysCollapsed"

您可以将其添加到您的 CollapsingToolbarLayoutenterAlwaysCollapsed CollapsingToolbarLayout 而不是 工具栏

更新:我在布局和 ID 中发现了一些问题。

例如,不确定您是否看到了,但我会解释一下。首先,Toolbar 的 id:id_toolbaronCreate 是:

Toolbar toolbar = (Toolbar) findViewById(R.id.main__toolbar);

所以,只需将其更改为:

<android.support.v7.widget.Toolbar
android:id="@+id/main__toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

P.s: 我刚刚删除了标题和 Scrollflag 标题来自 Manifest -> android:label 并且您需要更改它。(如果您想更改它,请以编程方式进行。)

and return immediately on scroll down (like most reading apps, g+ is an example also).

只需将它添加到您的工具栏:

app:layout_scrollFlags="scroll|enterAlways"

最后,在这里你可以看到我对这个问题的回答:https://stackoverflow.com/a/35241363/4409113

我们有:

enter image description here

更新:我没有使用:

android:fitsSystemWindows="true"

也在 CoordinatorLayout 中。正如您在此处看到的示例:

https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml

更新:要解决白色工具栏问题,请在 values-v21/styles.xml 上设置:

<item name="android:statusBarColor">@color/colorPrimaryDark</item>

这给出了适当的行为和外观。

关于android - 工具栏位于 Appbar 之上 - CoordinatorLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com