我有一个带有导航抽屉和全出血碎片的活动(顶部的图像必须出现在Lollipop上的半透明系统栏后面).虽然我有一个临时解决方案,其中片段通过简单地<fragment>在Activity的XML 中标记而膨胀,但它看起来很好.
然后,我不得不更换<fragment>与<FrameLayout>和执行片段交易,现在的片段不,出现在系统栏的后面了,尽管fitsSystemWindows被设置为true所有所需的层级.
我相信<fragment>在Activity的布局与其自身的夸大之间可能存在一些差异.我用Google搜索并为KitKat找到了一些解决方案,但这些解决方案都不适用于我(Lollipop).
activity.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
fragment.xml之
<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"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="224dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
...
Run Code Online (Sandbox Code Playgroud)
当activity.xml以这种方式工作时它起作用:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.actinarium.random.ui.home.HomeCardsFragment"
tools:layout="@layout/fragment_home"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud) 我正在尝试了解Android操作系统,当我阅读Google I/O 2014应用程序时,我遇到了WindowInsets.如果有人能解释他们是什么,那将是一个很大的帮助.谢谢.
android.view.WindowInsets 在API级别20中添加.
我导入android.view.WindowInsets我的CustomLayout覆盖onApplyWindowInsets(WindowInsets insets),但ClassNotFoundException发生在一些手机,其api级别从14到21.这是什么原因?
发生在:Rooted Nexus 5,Android 4.4.2
堆栈跟踪:
Fatal Exception: java.lang.NoClassDefFoundError: android/view/WindowInsets
at java.lang.Class.getDeclaredMethods(Class.java)
at java.lang.Class.getDeclaredMethods(Class.java:656)
at android.view.ViewDebug.getExportedPropertyMethods(ViewDebug.java:960)
at android.view.ViewDebug.exportMethods(ViewDebug.java:1047)
at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:997)
at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:983)
at android.view.ViewDebug.dumpView(ViewDebug.java:900)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:855)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dump(ViewDebug.java:793)
at android.view.ViewDebug.dispatchCommand(ViewDebug.java:416)
at android.view.ViewRootImpl$W.executeCommand(ViewRootImpl.java:6258)
at android.view.IWindow$Stub.onTransact(IWindow.java:65)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(NativeStart.java)
Caused by java.lang.ClassNotFoundException: Didn't find class "android.view.WindowInsets" on path: DexPathList[[zip file "/data/app/***-1.apk"],nativeLibraryDirectories=[/data/app-lib/***-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at …Run Code Online (Sandbox Code Playgroud) 该活动布局具有DrawerLayout与FrameLayout作为内容Fragment和LinearLayout被打开和关闭的抽屉。用于内容的片段有一个FrameLayoutwithfitsSystemWindows标志和一个Toolbar.
如果我onCreate()按预期插入片段,则从FrameLayout位置 0Toolbar开始,从状态栏下方开始。但是在onCreate()相同的代码之外失败,fitsSystemWindows不能与FrameLayout.
重要的一点,我需要打开Toolbar和fitsSystemWindows标记Fragment而不是打开Activity。
如何在onCreate()此刻看到相同的行为?
主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
window.setStatusBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity);
findViewById(R.id.item).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments navigation-drawer windowinsets