【问题标题】:Android findViewById(android.R.id.home) return null with support libraryAndroid findViewById(android.R.id.home) 使用支持库返回 null
【发布时间】:2014-12-24 16:56:18
【问题描述】:

我想更改 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>

【问题讨论】:

  • 你试过用 MenuItem 代替 ImageView 吗?
  • @RenanLopesFerreira 在任何情况下都会返回 null
  • 您找到解决方案了吗?我也面临同样的问题。
  • 我面临同样的问题。如果您发现了什么,愿意分享吗?
  • @AndreiN。没找到

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


【解决方案1】:

嗯,你没看错,如果你查看你继承的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) - 这是有道理的正如前面的链接所示,app compat 在底层不使用Toolbar,旧版实现不会使用。


作为一项测试,您可以按照评论中所说的操作并覆盖 onOptionsItemSelected 按徽标并查询您通过的视图以找到它的 id getId()

【讨论】:

  • 当我得到项目时只有一种方法可以得到View - getActionView(),它返回null
【解决方案2】:

显然,'home' 是您的布局文件的名称? [不,我看到那是 activity_main。] 要访问 relativelayout 项,它需要一个自己的 ID,例如 android:id="@+id/home"

【讨论】:

  • no android.R.id.home - android ActionBar 中主页按钮的 ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
相关资源
最近更新 更多