Android Navigation Drawer and windowActionBarOverlay = true

前端 未结 5 1710
清酒与你
清酒与你 2021-01-30 14:30

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 suba

5条回答
  •  无人共我
    2021-01-30 14:55

    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.

    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.

    Here's what I did:

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

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

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

    • Create a custom drawable that has a top part transparent, and then rest of a solid color:

    It looks somehow like this:

    
    
        
            
        
    
    
    
        
            
        
    
    

    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.

    • For values: 48dp
    • For values-land: 40dp
    • For values-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.

    enter image description here

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

提交回复
热议问题