【问题标题】:CoordinatorLayout blank space under toolbar工具栏下的CoordinatorLayout空白
【发布时间】:2019-03-17 17:47:16
【问题描述】:

当我在虚拟设备上启动我的应用程序时,它就像一个 ekstra 工具栏出现在我的活动中。 它是一个带有嵌套滚动视图和折叠工具栏的活动。所有动画和所有按钮都完美运行,但它似乎增加了 ekstra 空间,并且它的大小正好是折叠/常规工具栏的大小。

如果我将 coordinatorLayout layout_height 更改为“wrap_content”,多余的空间将位于屏幕底部,而不是在正确的工具栏下方。

谁能看出问题所在?我现在尝试了很多不同的设置。

Non collapsed toolbar with imageview Collapsed toolbar

这是活动的代码:

public class algorithms_abcde extends AppCompatActivity {
    CollapsingToolbarLayout collapsingToolbarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_algorithms_abcde);

        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        collapsingToolbarLayout = findViewById(R.id.collapsingToolbar);
        collapsingToolbarLayout.setTitle(getString(R.string.btn_txt_abcde));
        collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        //Inflate the menu; This adds it to the actionba
        getMenuInflater().inflate(R.menu.menu_other, menu);
        return true;
    }

    // Determines which button has been pressed.
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        //Switch case to handle item presses on the action bar
        switch (item.getItemId()) {

            case android.R.id.home:
                this.finish();
                return true;

            case R.id.action_home:
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是活动的xml:

<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"
    tools:context=".algorithms_abcde"
    android:layout_gravity="bottom">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:contentScrim="@color/colorVeryLight"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            android:fitsSystemWindows="true"
            >

            <ImageView
                android:id="@+id/headLine"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:layout_marginLeft="60dp"
                android:layout_marginRight="60dp"
                android:fitsSystemWindows="true"
                android:src="@drawable/img_abcde_headline"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                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"
        android:layout_gravity="bottom"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="@android:color/white">

        <TextView
            android:id="@+id/scrollAbleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:padding="10dp"
            android:text="@string/stringtest"/>

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

这是themes.xml的xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="windowActionBar">false</item>
        <item name="android:buttonStyle">@style/button</item>
        <!-- Base application theme. -->
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:background">@color/colorVeryLight</item>
    </style>

</resources>

最后是清单:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".algorithms_opqrst"></activity>
        <activity android:name=".algorithms_abcde" />
        <activity android:name=".algorithms_activity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 尝试从 CoordinatorLayout 中移除 android:layout_gravity="bottom"
  • 遗憾的是这不起作用。实际上它根本没有对活动做任何事情

标签: android toolbar android-coordinatorlayout android-collapsingtoolbarlayout


【解决方案1】:

我找到了答案。 我必须删除所有“android:fitsSystemWindows="true"”并将其添加到 CoordinatorLayout。 然后我还必须从 CoordinatorLayout 和 NestedScrollView 中删除“android:layout_gravity="bottom"”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多