我想改变ActionBar主页按钮左边填充.我试过
this解决方案.但是当我尝试findViewById(android.R.id.home)时,我得到null.同时android.R.id.home不是0.
只有当我使用android-suppot-v7时才会发生这种情况.如果我不使用支持库一切顺利.
也许有人可以帮助我?
这是我的简单代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
ImageView view = (ImageView)findViewById(android.R.id.home);
if (view !=null){
view.setPadding(10, 0, 0, 0);
}
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.timoshkois.actionbar.MainActivity">
</RelativeLayout>
最佳答案 嗯,你没错,如果你查看你继承的Activity的源代码,他们也会使用android.R.id.home
像这样
@Override
public final boolean onMenuItemSelected(int featureId, android.view.MenuItem item) {
if (super.onMenuItemSelected(featureId, item)) {
return true;
}
final ActionBar ab = getSupportActionBar();
if (item.getItemId() == android.R.id.home && ab != null &&
(ab.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
return onSupportNavigateUp();
}
return false;
}
看看ActionBar是如何创建的,它使用以下类:
这导致了ActionBar类,这有可能为什么它返回null
/**
* Standard navigation mode. Consists of either a logo or icon
* and title text with an optional subtitle. Clicking any of these elements
* will dispatch onOptionsItemSelected to the host Activity with
* a MenuItem with item ID android.R.id.home.
*
* @deprecated Action bar navigation modes are deprecated and not supported by inline
* toolbar action bars. Consider using other
* <a href="http://developer.android.com/design/patterns/navigation.html">common
* navigation patterns</a> instead.
这个弃用意味着新的ToolBar不会使用导航模式,所以这也意味着工具栏将没有这个Android ID(R.id.home) – 这是有道理的,因为之前的链接显示app compat不使用工具栏在引擎盖下,哪些遗留实现将不会使用.
作为测试,您可以执行注释所说的内容并覆盖onOptionsItemSelected按徽标并查询您传递的视图以查找它的id getId()