如何在应用程序标题旁边有一个后退按钮?
How to have a back button beside the app title?
如何在应用标题旁边添加后退按钮?
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);
}
后退按钮显示在右侧hand-side。 如何将其移动到left-hand一侧?
create-back
<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>
I can create a back icon, just don't know how to move it to left-hand
side.
在 Activity
的 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;
}
将大小写 R.id.back: 更改为 大小写 R.id.home: .
你的问题已经解决了。
已更新
使用此代码获取左侧的图标,
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);
}
为了使图标正常工作,只需添加此
@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);
}
如何在应用标题旁边添加后退按钮?
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);
}
后退按钮显示在右侧hand-side。 如何将其移动到left-hand一侧?
create-back
<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>
I can create a back icon, just don't know how to move it to left-hand side.
在 Activity
的 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;
}
将大小写 R.id.back: 更改为 大小写 R.id.home: .
你的问题已经解决了。
已更新
使用此代码获取左侧的图标,
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);
}
为了使图标正常工作,只需添加此
@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);
}