根据 Google's document , getActionBar().setDisplayHomeAsUpEnabled(true)
需要显示向上按钮。我使用 Eclipse 中的向导创建了一个简单的 Activity 并指定了它的父 Activity 。我在自动生成的代码中找不到任何 getActionBar().setDisplayHomeAsUpEnabled(true)
,但是当此 Activity 启动时向上按钮存在并且按预期工作。任何人都可以阐明这一点吗?
public class FooActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foo);
//more code...
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//more code...
}
@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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//more code...
return rootView;
}
}
}
最佳答案
当您在 AndroidManifest
中指定 parentActivityName
时,Activity
will check for that and automatically enable the "up" affordance if it's present.
关于android - 为什么在没有 setDisplayHomeAsUpEnabled() 的情况下出现向上按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812669/