2017-02-21 110 views
1

实际结果:状态栏显示在操作栏Toolbar上方,并且操作栏内的MenuItem被切断。注意:“测试”是操作栏的title透明状态栏与操作栏重叠

JankActionBar

预期结果: 绑定操作栏Toolbar的顶部应直接出现结合的状态栏的底部的下方,任何MenuItem S上的动作杆内应该是完全可见的。

活动的XML布局:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout_root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/image_background" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop" 
     android:src="@drawable/background" 
     tools:ignore="ContentDescription"/> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/action_bar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@android:color/transparent" 
     android:fitsSystemWindows="true" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

</FrameLayout> 

操作栏titleMenuItem在运行时添加。

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

    setSupportActionBar((Toolbar) findViewById(R.id.action_bar)); 
    ActionBar actionBar = getSupportActionBar(); 
    assert actionBar != null; 
    actionBar.setDisplayHomeAsUpEnabled(false); 
    actionBar.setTitle("Test"); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_test, menu); 
    return true; 
} 

我添加fitsSystemWindows="true"Toolbar视图之前,状态栏仍覆盖在操作栏,但“SKIP” MenuItem被垂直居中操作栏中,使其部分地显示的状态栏下方。我预计fitsSystemWindows=true标志会给我预期的结果(如上所述),但它没有。就好像fitsSystemWindows="true"正确定位了“SKIP”按钮,但没有调整操作栏本身的位置。任何人都知道这可能是什么问题?

编辑:我意识到我可以删除fitsSystemWindows="true"并将marginTop="...statusBarHeight"添加到Toolbar视图,但我正在寻找一种更简洁的方式来解决此问题。

+0

可以请你分享完整的布局文件吗?和布局预览图像?我不能在布局中看到测试和跳过按钮代码。 – rahul

+0

这是否发生在其他布局? –

+0

@rahul,也许Ryan以编程方式添加标题和菜单项,而不是xml –

回答

0

尝试将您的工具栏分成AppBarLayout

像这样:

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

<android.support.v7.widget.Toolbar 
     android:id="@+id/action_bar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@android:color/transparent" 
     android:fitsSystemWindows="true" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

<AppBarLayout/> 
3

我的问题是由于我设置Toolbarlayout_height?attr/actionBarSize。我原本以为fitsSystemWindows重新定位Toolbar,但它似乎添加了填充。所以当顶部填充被添加到Toolbar时,Toolbar的内容被下压。由于Toolbar的高度是固定的,内容物被推到容器的下边界以下。我最终设置了?attr/actionBarSize作为ToolbarminHeight属性的值来解决这个问题。下面是我的更新Toolbar

<android.support.v7.widget.Toolbar 
    android:id="@+id/action_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@android:color/transparent" 
    android:fitsSystemWindows="true" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

买者:这工作,如果你不想直接显示在下面,因为操作栏的高度操作栏说三道四,由于某种原因,大约是高度的两倍它需要包含一行主页图标,标题和/或菜单项。如果有人知道实现非重叠状态栏和正常大小操作栏的方式,请分享您的洞察力。我会永远感激。

HugeActionBar

买者更新:好的。所以显然我的动作栏正在接收额外的底部填充,相当于导航栏的高度,因为我在Activity的主题上设置了<item name="android:windowTranslucentNavigation">true</item>。我通过删除windowTranslucentNavigation属性来验证这一点。我正在使用Android支持库v25.1.1在7.1像素上进行测试。

+0

添加了一个警告... – Ryan

+1

对于需要使用'layout_height' ='?attr/actionBarSize'的正常尺寸操作栏的屏幕,此解决方案不存在。我还没有找到解决方案。 :/ – Ryan

+0

什么帮助我保持状态栏半透明(与工具栏几乎相同的颜色)和合适大小的工具栏,是将所有活动内容放入xml中的协调器布局中。 – shtolik