Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

android - ActionBar with support library and Fragments overlay content

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+ and also on android 2.3 with a normal Activity that has setContentView in onCreate, but when the activity is loading an Fragment in onCreate the ActionBar gets overlapped with the content of my layout. At all other scenarios the ActionBar works well.

Here is some code:

class AssetsActivity extends ActionBarActivity{
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);

        OpenLocalFragment assets = OpenLocalFragment.newInstance(index);
            assets.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, assets).commit();
    }
 }

The theme of this activity is set in the manifest to:

@style/Theme.AppCompat

An this is the result on android 2.3 (on 4.0+ the ActionBar shows well) actionBar overlaping

You can se that the first lisview item is overlaping the ActionBar(White round icon and title "My activity")

It is possible that i found a bug in the support library, it is released only for 2 days now? Thanks to all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...