The Action bar above
- The Action bar above
- Brief introduction
- Body
- Extended Reading
Target group: Android Beginner without foundation
Knowledge Point: Use the support V7 package in Android Studio to achieve Actionbar effects
Goal: Display a actionbar on the page
Brief introduction
Body
1. First we need to add a reference to the support V7 package in the Build.gradle code as follows
dependencies { ... ‘com.android.support:appcompat-v7:21.0.3‘ ...}
- You can also select the open Module settings-dependencies-in the project and click on the right plus-library dependency-select Appcompat-v7 to add
2. Create an activity page and make it inherit android.support.v7.app.ActionBarActivity, the code is as follows
import android.os.Bundle;import android.support.v7.app.ActionBarActivity;publicclass MainActivity extends ActionBarActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
- In order to fit a low-version Android, the actionbaractivity must come from Android.support.v7.app
3. Specify a Theme.appcompat theme (or inherit the subject) for the above page, for example:
<activity android:theme="@style/Theme.AppCompat.Light"... >
- tips from the official : in API 11 and later, Actionbar will default to all Theme.holo themes, and if you don't need it, you can set the activity theme to Theme.Holo.NoActionBar
4. How to get a Actionbar object and some simple property modifications
ActionBar actionBar = getSupportActionBar();//使应用图标左侧出现一个按钮actionBar.setDisplayHomeAsUpEnabled(true);//使ActionBar隐藏actionBar.hide();//为ActionBar设置标题actionBar.setTitle("大家好");
Extended Reading
- More ways to use Actionbar
Android Basics (15)