2014-12-24 2 views
2

ActionBar 홈 버튼 왼쪽 패딩을 변경하고 싶습니다. 나는 this 해결책을 시도했다. 하지만 findViewById(android.R.id.home)을 시도하면 null이됩니다. 같은 시간에 android.R.id.home은 0이 아닙니다. 그리고 이것은 android-suppot-v7을 사용하는 경우에만 발생합니다. 지원 라이브러리를 사용하지 않으면 모든 것이 잘됩니다. 누군가 나를 도울 수 있습니까? 여기안드로이드 findViewById (android.R.id.home)는 지원 라이브러리와 함께 null을 반환합니다.

내 간단한 코드입니다 :

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setHomeButtonEnabled(true); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setDisplayShowHomeEnabled(true); 
     ImageView view = (ImageView)findViewById(android.R.id.home); 
     if (view !=null){ 
     view.setPadding(10, 0, 0, 0); 
     } 
    } 
} 

레이아웃 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.timoshkois.actionbar.MainActivity"> 

</RelativeLayout> 
+0

입니다 찾기 위해 전달되는보기를 주석 말하는 일을하고 onOptionsItemSelected 눌러 로고를 무시하고 조회 할 수있는 테스트로


는 대신 이미지 뷰의 MenuItem을 사용하여 시도 적이 있습니까? –

+0

@RenanLopesFerreira 어쨌든 null을 반환합니다 –

+0

해결 방법을 찾으셨습니까? 나는 똑같은 문제에 직면 해있다. – AndroidGuy

답변

0

은 분명히 '가정'레이아웃 파일의 이름입니다? [아니오, 저것은 activity_main입니다.] relativelayout 항목에 액세스하려면 android : id = "@ + id/home"과 같은 자체 ID가 필요합니다.

+1

안드로이드 안드로이드 ActionBar –

2

흠 당신이에서 상속 Activity의 소스, 그들은 또한 액션 바는 그것을 만드는 방법을 찾고이

@Override 
public final boolean onMenuItemSelected(int featureId, android.view.MenuItem item) { 
    if (super.onMenuItemSelected(featureId, item)) { 
     return true; 
    } 
    final ActionBar ab = getSupportActionBar(); 
    if (item.getItemId() == android.R.id.home && ab != null && 
      (ab.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) { 
     return onSupportNavigateUp(); 
    } 
    return false; 
} 

처럼 android.R.id.home

https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v7/appcompat/src/android/support/v7/app/ActionBarActivity.java

를 사용하여 이러한 클래스를 사용

액션 바 클래스로 연결

https://github.com/android/platform_frameworks_support/blob/5476e7f4203acde2b2abbee4e9ffebeb94bcf040/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java

https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/WindowDecorActionBar.java

,이 '

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/ActionBar.java#L106

/** 
* Standard navigation mode. Consists of either a logo or icon 
* and title text with an optional subtitle. Clicking any of these elements 
* will dispatch onOptionsItemSelected to the host Activity with 
* a MenuItem with item ID android.R.id.home. 
* 
* @deprecated Action bar navigation modes are deprecated and not supported by inline 
* toolbar action bars. Consider using other 
* <a href="http://developer.android.com/design/patterns/navigation.html">common 
* navigation patterns</a> instead. 

이 중단 새로운 툴바 원 의미는 null를 돌려 이유에 대해 가능한 단서를 가지고 탐색 모드를 사용하십시오. 이것은 툴바가이 Android ID (R.id.home)를 가지지 않음을 의미합니다. 이는 이전 링크가 앱 compat이 사용하지 않는 Toolbar을 사용함을 보여주기 때문에 의미가 있습니다. 당신이 당신이 아이디 getId()

+0

에서'android.R.id.home' 안드로이드 홈 버튼의 ID를 찾지 못했습니다.'View' - getActionView()를 얻기위한 유일한 방법이 있었는데'null ' –

관련 문제