12

我正在尝试自定义与(我正在使用 ActionBarSherlock)contentDescription的向上按钮关联的“向上导航”默认值。ActionBar

来自ActionBarView的来源:

public void setHomeButtonEnabled(boolean enable) {
    mHomeLayout.setEnabled(enable);
    mHomeLayout.setFocusable(enable);
    // Make sure the home button has an accurate content description for accessibility.
    if (!enable) {
        mHomeLayout.setContentDescription(null);
    } else if ((mDisplayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
        mHomeLayout.setContentDescription(mContext.getResources().getText(
                R.string.abs__action_bar_up_description));
    } else {
        mHomeLayout.setContentDescription(mContext.getResources().getText(
                R.string.abs__action_bar_home_description));
    }
}

所以关键是如何获得对mHomeLayout. getWindow().getDecorView().findViewById(android.R.id.home)不工作,因为它返回一个 ImageView。

我该怎么办?

谢谢 ;)

4

4 回答 4

14

布局

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:layout_scrollFlags="scroll|enterAlways">

</android.support.v7.widget.Toolbar>

代码

public Toolbar toolbar;
...
setContentView(layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(layoutTitle);
setSupportActionBar(toolbar);
...
getSupportActionBar().setHomeActionContentDescription("Go Back To XYZ Screen");
于 2016-02-29T10:24:57.647 回答
7

在 xml 中,使用“navigationContentDescription”

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:layout_alignParentTop="true"
    app:navigationContentDescription="@string/back"/>
于 2020-02-18T21:39:01.680 回答
4

在这里,我可以如何在以前的项目中做你需要的事情:

((View) getWindow().getDecorView().findViewById(android.R.id.home).getParent().getParent()).setContentDescription("blablabla");

使用 viewHierarchy 插件可以帮助我理解 ActionBar 布局是如何构建的。

于 2013-08-26T11:49:23.063 回答
1

如果有人需要为 UIAutomator 设置 ActionBar 的主页按钮的内容描述请使用

((View) getWindow().getDecorView().findViewById(android.R.id.home).getParent()).setContentDescription("MANUALLYSET-home-up");

并使用访问UIAutomatorTestCase中的视图

new UiObject(new UiSelector().description("MANUALLYSET-home-up").className("android.widget.FrameLayout"));

由于某种原因,额外的 *.getParent() 不起作用,而是 Android 使用一些自动生成的内容描述值来为该父级,这在某些 Android 版本中可能不同(例如,KITKAT 上的“app_name,关闭导航抽屉”和“app_name,在 JELLYBEAN 上向上导航)。幸运的是,访问它的孩子也可以。

亲切的问候

于 2014-02-27T08:56:04.437 回答