就知道图像中的 ListView 是 fragment 生成的。但我认为这不会干扰工具栏的布局。这是布局代码 -
<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>
这里是上面布局对应的 Activity -
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
这是我的主要 Activity 。它还加载 fragment ListView 和自定义工具栏。但这里没有这样的空白 -
@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
}
}
另请注意,我在两个 Activity 中替换了相同的 R.id.frameLayout,但这应该与工具栏没有任何关系。谁能告诉我为什么它没有发生在 MainActivity 中而是发生在其他 Activity 中。
最佳答案
问题实际上不在于工具栏,而在于状态栏。它需要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>
关于java - 工具栏和状态栏之间的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015948/