【问题标题】:Set contentDescription for ActionBar's up button为 ActionBar 的向上按钮设置 contentDescription
【发布时间】:2013-05-21 10:50:40
【问题描述】:

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

来自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。

我该怎么办?

谢谢 ;)

【问题讨论】:

    标签: android android-actionbar actionbarsherlock accessibility


    【解决方案1】:

    布局

    <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");
    

    【讨论】:

    • 有没有办法用样式改变描述?
    【解决方案2】:

    在 xml 中,使用“navigationContentDescription”

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_alignParentTop="true"
        app:navigationContentDescription="@string/back"/>
    

    【讨论】:

      【解决方案3】:

      我可以在这里做你在以前的项目中需要的东西:

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

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

      【讨论】:

        【解决方案4】:

        如果有人需要为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,关闭导航抽屉”和JELLYBEAN 上的“app_name,向上导航”)。幸运的是,访问它的孩子也可以。

        亲切的问候

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多