5

我试图自定义与向上按钮ActionBar(我使用ActionBarSherlock)关联的“向上导航”默认contentDescription为ActionBar的向上按钮设置contentDescription

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

所以关键是如何获得一个参考mHomeLayoutgetWindow().getDecorView().findViewById(android.R.id.home)无法正常工作,因为它正在返回一个ImageView。

我该怎么办?

感谢;)

回答

4

在这里,我该怎么办,你在以前的项目所需要的:

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

使用viewHierarchy插件可以帮助我理解动作条的布局是如何建立。

0

万一有人需要设置ActionBar的家按钮的内容描述为UIAutomator,使用

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

,并使用

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

出于某种原因,额外的访问您UIAutomatorTestCase视图* .getParent()不起作用,相反,Android使用一些自动生成的内容描述值,该值可能在某些Android版本中不同(例如KITKAT上的“app_name,Close navigation drawer”和JEL上的“app_name,Navigate up” LYBEAN)。幸运的是,接触其孩子也很有效。

亲切的问候

4

布局

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