如何在 appcompat.v7 中获取主页视图

How to get home view in appcompat.v7

我正在使用 ActionBarActivity 并尝试使用以下方法获取主页按钮(汉堡包)的位置:

View home = findViewById(android.support.v7.appcompat.R.id.home);

and/or

View home = findViewById(android.R.id.home);

问题是我总是得到空值。 任何人都知道如何获得视图?也许我试图从错误的地方得到它

我正在尝试从 OnCreateOptionsMenu(菜单菜单)获取它

这样做:

getActionBar().setHomeButtonEnabled(true);

View home = findViewById(android.R.id.home);

我遵循了 NikolasDespotoski 代码,我现在可以得到它

Code here

谢谢大家

为了避免以后代码消失,我只是从@rendellhb

提供的Gist复制过来的
public class ToolbarNavigationUtil {
    public static View getToolbarNavigationIcon ( Toolbar toolbar ){
        //check if contentDescription previously was set 
        boolean hadContentDescription = TextUtils. isEmpty(toolbar.getNavigationContentDescription());
        String contentDescription = !hadContentDescription ? toolbar.getNavigationContentDescription().toString() : "navigationIcon" ;
        toolbar . setNavigationContentDescription(contentDescription);
        ArrayList< View > potentialViews = new ArrayList< View >();
        //find the view based on it's content description, set programatically or with android:contentDescription 
        toolbar . findViewsWithText(potentialViews,contentDescription, View . FIND_VIEWS_WITH_CONTENT_DESCRIPTION );
        //Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existence 
        View navIcon = null ;
        if (potentialViews . size() > 0 ){
            navIcon = potentialViews . get( 0 ); //navigation icon is ImageButton 
        }
        //Clear content description if not previously present 
        if (hadContentDescription)
            toolbar . setNavigationContentDescription( null );
        return navIcon;
    }

    public static TextView getToolbarTitleView ( ActionBarActivity activity , Toolbar toolbar ){
        ActionBar actionBar = activity . getSupportActionBar();
        CharSequence actionbarTitle = null ;
        if (actionBar != null )
            actionbarTitle = actionBar . getTitle();
        actionbarTitle = TextUtils . isEmpty(actionbarTitle) ? toolbar . getTitle() : actionbarTitle;
        if ( TextUtils . isEmpty(actionbarTitle)) return null ;
        // can't find if title not set 
        for ( int i = 0 ; i < toolbar . getChildCount(); i ++ ){
            View v = toolbar . getChildAt(i);
            if (v != null && v instanceof TextView ){
                TextView t = (TextView) v;
                CharSequence title = t . getText();
                if ( ! TextUtils . isEmpty(title) && actionbarTitle . equals(title) && t . getId() == View . NO_ID ){
                    //Toolbar does not assign id to views with layout params SYSTEM, hence getId() == View.NO_ID 
                    //in same manner subtitle TextView can be obtained. 
                    return t;
                }
            }
        }
        return null ;
    }
} 

这是我的解决方案,对我有用。

setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
Log.d("checksize",toolbar.getChildCount()); \print 1, this child is title
setSupportActionBar(toolbar);
Log.d("checksize",toolbar.getChildCount()); \print 2
View navIcon = toolbar.toolbar.getChildAt(1);

这对我有用:

  1. 使用扩展“NoActionBar”的样式作为 activity 的主题
  2. 在 activity 的布局文件中创建工具栏

然后这样查询home键

使用 Kotlin: val image = toolbar.getChildAt(1) 作为 AppCompatImageButton

使用Java: AppCompatImageButton imageButton = (AppCompatImageButton)toolbar.getChildAt(1)