问题
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?
回答1:
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);
}
}
回答2:
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
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/32310681/how-to-add-actionbaractivity-in-android-project