Android Basics (15)

Source: Internet
Author: User

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
    • Support for V7 package import

    • Simple use of Actionbar

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
    1. More ways to use Actionbar

Android Basics (15)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.