[英]white space between toolbar and status bar
只是知道圖像中的列表視圖是片段生成的。 但是我不認為這會干擾工具欄的布局。 這是布局代碼-
<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
}
}
我檢查了堆棧溢出中大多數與空白工具欄相關的答案,但沒有任何效果。 也許我的問題不同。
編輯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中發生,而在其他活動中發生。
問題實際上並非與工具欄有關,而是與狀態欄有關。 它需要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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.