2013-08-14 4 views
3

다음 코드로 작업 표시 줄의 배경을 변경하려고 시도했습니다. 4.3 이하에서는 작동하지만 4.3 이하에서는 작동하지 않습니다. 다음 코드를 사용하면 널 배경이 설정됩니다. 즉. 오래된 배경이 제거되었지만 새 배경이 설정되지 않습니다. 도와주세요.작업 표시 줄 배경이 변경되지 않습니다.

public class TestActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.testing); 
    } 

    /** 
    * Callback when button is clicked to change background 
    * @param v 
    */ 
    public void onStartClicked(View v) { 
     int Min = 0; 
     int Max = 2; 

     //Random number generator between 0 and 2 inclusive 
     int pos = Min + (int) (Math.random() * ((Max - Min) + 1)); 

     if (pos == 0) { 
      getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.header)); 
     } else if (pos == 1) { 
      getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.inbox_header)); 

     } else if (pos == 2) { 
      getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.outbox_header)); 

     } 

    } 
} 
+0

이 라인을 작성하십시오 - Drawable tmp = getResources(). getDrawable (R.drawable.outbox_header) 그리고 디버그하십시오. tmp는 null입니까? –

+0

BitmapDrawable을 배경으로 사용하고 있습니까? – gunar

+0

내 S3 (Android 4.1.2)에서 정상적으로 작동 중입니다. –

답변

21

생각합니다. 배경을 설정 한 후 작업 표시 줄의 제목을 표시하고 숨김으로써 눈치 챘습니다.

getActionBar().setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.inbox_header)); 
actionBar.setDisplayShowTitleEnabled(true); 
actionBar.setDisplayShowTitleEnabled(false); 

감사합니다.

+0

Arun이 제공하는 솔루션은보다 깨끗합니다. 액션 바를 무효화하는 두 가지 작업을 수행하고 있습니다. – gunar

+0

나는 이미 옵션 메뉴를 무효화하려고 시도했다. 그것은 나를 위해 작동하지 않았다. – Dipendra

+0

무효화는 저에게는 효과가 없었지만 꽤 효과적이지는 않았습니다. 감사합니다. – Tony

2

은 마지막으로, 나는 해결책을 발견 .. 당신이 액션 바 배경을 설정 한 후 invalidateOptionsMenu()를 호출하는 것입니다 할 수있는

public void onStartClicked(View v) { 
     int Min = 0; 
     int Max = 2; 

     //Random number generator between 0 and 2 inclusive 
     int pos = Min + (int) (Math.random() * ((Max - Min) + 1)); 

     if (pos == 0) { 
      getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.header)); 
     } else if (pos == 1) { 
      getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.inbox_header)); 

     } else if (pos == 2) { 
      getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.outbox_header)); 

     } 
     invalidateOptionsMenu(); 
    } 
+0

"invalidateOptionsMenu()"를 (를) 완벽하게 처리했습니다! – GFPF

관련 문제