2014-10-14 3 views
0

AppCompact v7의 Support Action Bar를 사용하기 만합니다. 나는 (사실) bar.setDisplayHomeAsUpEnabled를 작성하고이 버튼을 듣고 싶은,하지만 리스너에서 해요 :Listener Home 및 HomeAsUp 버튼이 지원 작업 표시 줄에서 작동하지 않습니다.

public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    switch (id) { 
    case R.id.action_settings: 
     return true; 
    case R.id.HomeAsUp: 
     return true; 
    default: 
     break; 
    } 

    return super.onOptionsItemSelected(item); 
} 

내가 아이디, 메소드의 반환 아무것도에서 항목을 찾으려합니다. Support Action Bar로이 버튼을 듣는 방법?

+0

R.id.HomeAsUp 대신, android.id.R.home을 시도하십시오. – michaelcarrano

+0

감사합니다. 이 기능은 작동하지만 HomeAsUp 버튼은 어떻게됩니까? – user3418460

+0

나는 집을 보지 못했습니다. 유효한 안드로이드라고. R.id ...이게 당신이 직접 만든 이드입니까? – michaelcarrano

답변

0
R.id.home - res id 2131034117 
    R.id.homeAsUp - res id 2131034132 
    android.R.id.home - res id 16908332 <-- HomeAsUpIndicator 

ActionBar.setHomeAsUpIndicator(getResIdFromAttribute(this, R.attr.homeAsUpIndicator)); 
ActionBar.setDisplayHomeAsUpEnabled(true); 

public static int getResIdFromAttribute(final Activity activity, final int attr) { 
    if (attr == 0) return 0; 
    final TypedValue typedvalueattr = new TypedValue(); 
    activity.getTheme().resolveAttribute(attr, typedvalueattr, true); 
    return typedvalueattr.resourceId; 
}