Android findViewById(android.R.id.home)返回带支持库的null

时间:2014-12-24 16:56:19

标签: android android-actionbar android-support-library

我想更改ActionBar主页按钮左侧填充。我尝试过this解决方案。但是当我尝试findViewById(android.R.id.home)时,我得到了空。同时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>

2 个答案:

答案 0 :(得分:2)

嗯,你没有错,如果你查看你继承的Activity的来源,他们也会使用android.R.id.home

https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v7/appcompat/src/android/support/v7/app/ActionBarActivity.java

喜欢这个

@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,它使用以下类:

https://github.com/android/platform_frameworks_support/blob/5476e7f4203acde2b2abbee4e9ffebeb94bcf040/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java

https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/WindowDecorActionBar.java

导致ActionBar类,这有一个关于它为什么返回null的可能线索

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/ActionBar.java#L106

/**
 * 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.

此弃用意味着新的工具栏不会使用导航模式,因此 可能 这也意味着工具栏不会有此Android ID(R.id.home) - 这是有道理的,因为之前的链接显示应用程序compat不使用引擎盖下的Toolbar,传统的实现将不会使用。


作为测试,您可以执行评论所说的内容并覆盖onOptionsItemSelected,按下徽标并查询您传递的视图,以查找其ID getId()

答案 1 :(得分:0)

显然,&#39; home&#39;是您的布局文件的名称? [不,我看到了&#39; s活动_主要。]要访问relativelayout项目,它需要一个自己的ID,如android:id =&#34; @ + id / home&#34;