如何在android studio中为初学者添加动作栏?

cha*_*aka 6 android android-layout android-actionbar android-studio

我想构建我自己的应用程序,我想为它添加一个操作栏.在我的主要布局中,我想要一个只有我的应用程序名称的操作栏.在我的其他布局/页面中,我想要我的页面名称和后面的导航符号导航回到上一页.任何人都可以告诉我如何做到这一点,也可以有人告诉我android中的默认操作栏它做了什么以及我该怎么做.

Kal*_*iya 11

第1步:build.gradle(应用程序级别)

dependencies {
    compile 'com.android.support:appcompat-v7:23.3.0'
}
Run Code Online (Sandbox Code Playgroud)

第2步:AndroidManifest.xml

<application
....
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"/>
Run Code Online (Sandbox Code Playgroud)

第3步:在您的活动中

public class main extends AppCompatActivity
{
    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...

        getSupportActionBar().setTitle("Your Activity Title"); // for set actionbar title
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // for add back arrow in action bar
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    int id = item.getItemId();
    if (id == android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)