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

问题描述 投票:0回答:1

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中,而是发生在其他活动中。

java android android-toolbar
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.