简体   繁体   中英

How to add ActionBarActivity in Android Project?

I'm not able to add ActionBarActivity in my Android project. There are no options of appcpmpat-v7 in any of my project.

What i do now?

public class MainActivity extends Activity {

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

    ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
    mTitleTextView.setText("My Own Title");

    ImageButton imageButton = (ImageButton) mCustomView
            .findViewById(R.id.imageButton);
    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onCl`enter code here`ick(View view) {
            Toast.makeText(getApplication`enter code here`Context(), "Refresh Clicked!",
                    Toast.LENGTH_LONG).show();
        }
    });

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
}

}

If this is a gradle based project then you can add the following dependency in your build.gradle file which will import this dependency.

compile 'com.android.support:appcompat-v7:23.0.0'

PS: 23.0.0 is the latest version targeting android M. You can use 21 or 22 as well

You should have dependency in your gradle file for com.android.support:appcompat-v7:21.0.0

And you mistyped name of class in question.

You can find the answer here How to add suport library,ActionBarActivity and themes to android project? If your workspace does not contain appcompat, it will be automatically made by Eclipse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM