ActionBar with support library and Fragments overlay content

后端 未结 4 891
梦毁少年i
梦毁少年i 2020-12-13 11:12

I added the android-support-library-v7-appcompat to my project to support ActionBar from API level 7 above.

It works like a charm on android 4.0+ an

相关标签:
4条回答
  • 2020-12-13 11:15

    Edit: This is now officially fixed and released in the Support Library v19.

    As JJD commented below, you can use normally android.R.id.content with appcompat-v7 r.19.0.0 or newer. The home button works too.

    With other words: The workaround below is no more needed if you use version 19.0.0 or newer.


    I got the answer at code.google.com. i've made a summary from frederic's answer:

    For pre ICS devices you must use:

    R.id.action_bar_activity_content

    instead of

    android.R.id.content

    R.id.action_bar_activity_content is a new id used in layout for displaying app content, it would appear that it replace android.R.id.content when you use support v7 appcompat ActionBarActivity.

    You can use this code to retrieve the correct id of the activity content :

    public static int getContentViewCompat() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
                   android.R.id.content : R.id.action_bar_activity_content;
    }
    

    Thanks to frederic

    0 讨论(0)
  • 2020-12-13 11:25

    Another alternative if you do not want to modify the source code of android-support-library-v7-appcompat is to add an empty layout in the layout xml file such as:

    <LinearLayout
        android:id="@+id/content_view"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    and make the fragment call to add to that layout instead:

    ft.add(R.id.content_view, mFragment, mTag);
    
    0 讨论(0)
  • 2020-12-13 11:27

    Add a extra empty list header with the size of the actionbar. Should be a good workaround.

    0 讨论(0)
  • 2020-12-13 11:29

    Seems a bit late to contribute but I had the same problem and haven't seen the answer here....

    Check your styles.xml file, there might be an xml attribute for overlaying the actionbar set to true ie

    true

    The whole entry looks something like this

        <item name ="actionBarTabTextStyle" > @style/TabTextStyle</item>
        <item name = "windowActionBarOverlay">true</item>
    

    If that is the case then just change the value of "windowActionBarOverlay" to false.

    0 讨论(0)
提交回复
热议问题