工具栏和状态栏之间的空白

时间:2019-03-06 05:11:28

标签: java android android-toolbar

enter image description here

所以知道图像中的列表视图是片段生成的。但是我不认为这会干扰工具栏的布局。这是布局代码-

    <RelativeLayout 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:id="@+id/fragment_songlistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/croctooth"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    tools:context="com.shaikhsakib.sounddemo.SongActivity">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />

    <ListView
        android:id="@+id/fragmentSongListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar2" />
</RelativeLayout>

这里是上述布局的相应活动-

    package com.shaikhsakib.sounddemo;

import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class SongActivity extends AppCompatActivity {

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

        loadFragment();
        Toolbar toolbar2 = (Toolbar) findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar2);

    }

    private void loadFragment() {
// create a FragmentManager
        FragmentManager fm;
        fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
        SongListFragment slv = new SongListFragment();
        fragmentTransaction.replace(R.id.songFrameLayout, slv);
        fragmentTransaction.commit(); // save the changes
    }
}

我检查了Stack Overflow上大多数与空白工具栏相关的答案,但没有任何效果。也许我的问题不同。

编辑1

这是我的主要活动。它还将加载片段列表视图和自定义工具栏。但是这里没有这样的空格-

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

setSupportActionBar(toolbar);

}

private void loadFragment() {
// create a FragmentManager
        FragmentManager fm;
        fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
        PLayListFragment flv = new PLayListFragment();
        fragmentTransaction.replace(R.id.frameLayout, flv);
        fragmentTransaction.commit(); // save the changes
    }
}

还请注意,我在两个活动中都替换了相同的R.id.frameLayout,但这与工具栏没有任何关系。谁能说出为什么它不在MainActivity中发生,而是在其他活动中发生。

1 个答案:

答案 0 :(得分:0)

问题实际上并非与工具栏有关,而是与状态栏有关。它需要CoordinatorLayout而不是RelativeLayout作为父项。因此,我要做的就是将相对布局作为CoordinatorLayout的子项,并传递android:fitsSystemWindows =“ true”之类的属性。那解决了问题。

这是解决方案-

<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"
    xmlns:tools="http://schemas.android.com/tools"
    android:fitsSystemWindows="true"
    android:background="@color/croctooth"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    tools:context="com.shaikhsakib.sounddemo.SongActivity">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />

    <ListView
        android:id="@+id/fragmentSongListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar2" />


</RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
相关问题