安卓导航抽屉和windowActionBarOverlay =真抽屉、windowActionBarOverlay

2023-09-12 02:24:57 作者:犯二的小痞子

我试图实施新的Andr​​oid导航抽屉在我的应用程序。我创建了处理抽​​屉设置和听众BaseActivity.java,和我有两个子活动扩展这个基类。关于第二个活动,我打算使用一个不同的动作栏样式,使用以下ATTRS:

I'm trying to implement the new Android Navigation Drawer in my application. I have created a BaseActivity.java that handles the Drawer setup and listeners, and I have two subactivities that extend this base class. On the second activity, I plan to use a different action bar style, using the following attrs:

<item name="android:windowActionBarOverlay">true</item>
<item name="android:background">@android:color/transparent</item>

,使操作栏透明,使内容更加丰富,因为在我的布局图象头。

to make the action bar transparent, and make content richer, as there is a picture header in my layout.

我已经实现了这一点,但现在的问题是,因为它的内容扩展到使用更多的动作条作为覆盖的额外空间的优势,抽屉式导航本身正在扩大也和它重叠的动作条,创一个pretty的可怕的寻找布局:

I've achieved just that, but now the problem is, that because the content is expanding to take advantage of the extra space of using the ActionBar as overlay, the Navigation Drawer itself is expanding too and it overlaps the ActionBar, creating a pretty awful looking layout:

我想什么都做,就是实际的内容(框架布局将填充一个片段)占用额外的空间,但资产净值抽屉还是去操作栏中,类似于播放下方音乐应用程序:

What I'd like to have done, is the actual content (frame layout that will be populated with a fragment) to take up the extra space, but have the nav drawer still go underneath the action bar, similar to the Play Music App:

这是我能做些什么来做到这一点任何想法?

Any ideas on what I can do to make that happen?

修改所以,按艾哈迈德的援助,我设置了 marginTop 仅在ListView。这里的布局:

EDIT So, as per Ahmad's assistance I set the marginTop on the ListView only. Here's the layout:

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
          android:layout_marginTop="?android:attr/actionBarSize"
<!-- This was added after seeing the crazy effect, but does nothing -->
          android:layout_marginBottom="0dp"
          android:layout_marginLeft="0dp"
          android:layout_marginRight="0dp"
          android:layout_width="240dp"
          android:layout_height="fill_parent"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:background="?attr/listviewBackground"
          />

而现在,它的伟大工程的顶面,但由于某些原因,也有一个保证金的观点,这没有任何意义,我在所有的底部。 下面是截图。

不知道是什么原因引起的:(

Not sure what's causing it :(

推荐答案

如果有人有兴趣另取了这个问题。这里是发生了什么事。

In case anyone is interested in another take to this question. Here's what happened.

我试过只缘设置这样的列表视图的顶部:

I tried setting only the margin to the top of the list view like this:

android:layout_marginTop="?android:attr/actionBarSize"

但是如前所述的编辑问题,即有一个怪异的行为,其中也有一个在底部的边距尽管不是在布局上的资源文件中设置。

But as mentioned on the edited question, that had a weird behaviour where there was also a margin on the bottom despite not being set on the layout resource file.

所以,我一直在寻找密切关注Play音乐应用,发现它不是真正的余量,而是一些填充,另外他们用的是自定义背景填充的填充用透明的颜色在指定的空间。

So, I was looking closely at the Play Music App and noticed that it's not actually a margin, but rather some padding, and additionally they are using a custom background that fills the space specified by the padding with a transparent color.

下面是我所做的:

设置填充的ListView控件的顶部,而不是保证金:

Set Padding at the top of the ListView, rather than margin:

的android:paddingTop =机器人:ATTR / actionBarSize

正如前面所说,重要的是为C尺寸不难$ C $,因为他们每个设备不同而不同。

As said before, it's important to not hard code the dimensions as they vary per device.

创建一个自定义绘制,有一个顶部透明,然后休息纯色的:

看起来在某种程度上是这样的:

It looks somehow like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape  android:shape="rectangle">
        <solid android:color="#80000000" />
    </shape>
</item>

<item android:top="@dimen/action_bar_default_height">
    <shape
            android:shape="rectangle">
        <solid android:color="@color/light_gray" />
    </shape>
</item>

请注意,我试图用安卓?ATTR / actionBarSize 的绘制,但所取得的应用程序强制关闭。相反,我搜索过的grep code,发现不同大小的操作栏的几个文件扪,所以我说这些我自己的项目的地扪文件。

Note that I tried to use ?android:attr/actionBarSize on the drawable, but that made the app force close. Instead, I searched through grepcode and found a few dimen files with different sizes for the action bar, so I added those to my own project's dimen files.

对于值:48dp 对于价值观的土地:40dp 对于价值观sw600dp:56dp

在这之后,我觉得我看起来不错,通知的截图如何在列表视图和动作条不重叠,以及列表视图的透明部分的大小正合适。

And after that, I think I looks great, notice on the screenshot how the listview and the actionbar don't overlap, and the transparent part of the listview is just the right size.

希望帮助的人谁​​不知道如何实现这一目标。

Hope that helps anyone who was wondering how to achieve this.