如何在应用标题旁边有一个后退按钮?

时间:2015-12-19 08:28:23

标签: android android-layout android-studio android-actionbar back

如何在应用标题旁边设置后退按钮?

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
}

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.create_back, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId()) {
            case R.id.back: // back to previous activity
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

后退按钮显示在右侧。 如何将其移至左侧?

创建回

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:icon="@mipmap/back_to"
    android:id="@+id/back"
    android:orderInCategory="100"
    android:title="Back"
    app:showAsAction="always" />
    </menu>

enter image description here

  

我可以创建一个后退图标,但不知道如何将其移动到左侧   侧。

3 个答案:

答案 0 :(得分:3)

将案例R.id.back改为案例R.id.home:

你的问题已经解决了。

<强>更新

使用此代码在leftside上获取图标,

final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


 @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            switch(item.getItemId()) {
                case R.id.home: // back to previous activity
                    onBackPressed();
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }

答案 1 :(得分:1)

在活动<创建

的oncreate()中使用以下代码
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);

添加动作

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
        onBackPressed();

        Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
        break;

    }

    return true;
}

答案 2 :(得分:0)

为了使图标有效,只需添加此

即可
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        onBackPressed();
        return true;
    }

而不是

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            switch(item.getItemId()) {
                case R.id.home: // back to previous activity
                    onBackPressed();
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }