2012-06-12 32 views
11

我試圖從處理程序更改ActionBar的背景。最終目標是將Handler傳遞給AsyncTask,但是現在,即使從Thread中調用Handler.sendMessage()也會產生奇怪的行爲。通過調試器,我可以看到Handler接收到消息,然後運行setActionBarBackground()直到最後。ActionBar setBackgroundDrawable()從線程/處理程序清零背景

帶藍色底部筆觸的默認ActionBar完全從屏幕上消失,並且不會被新的GradientDrawable替換。我懷疑背景是某種程度上無效的。此外,當我再次關注EditText時,會出現正確的GradientDrawable ActionBar背景。我期望的行爲是在後臺對actionDone進行簡單更改。

任何有關爲什麼會發生這種情況的見解將不勝感激!

相關代碼:

TestActivity.java

public class TestActivity extends RoboSherlockFragmentActivity { 

    @InjectView(R.id.ET_test) EditText testET; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.i(MainApp.TAG, "onCreate"); 
     setContentView(R.layout.test_activity); 

     testET.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { 
       if (i == EditorInfo.IME_ACTION_DONE) { 
        String loc = testET.getText().toString(); 
        InputMethodManager mgr = (InputMethodManager) getSystemService(
          Context.INPUT_METHOD_SERVICE); 
        mgr.hideSoftInputFromWindow(testET.getWindowToken(), 0); 
        Toast.makeText(TestActivity.this, "EditText done!", Toast.LENGTH_SHORT).show(); 

        /*TestQuery tq = new TestQuery(TestActivity.this, mHandler); 
        tq.execute();*/ 
        new Thread(new Runnable() { 
         public void run() { 
          try { 
           Thread.sleep(1000); 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 
          mHandler.sendMessage(new Message()); 
         } 
        }).start(); 
       } 
       return true; 
      } 
     }); 
    } 

    private Handler mHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      //setActivityColors(); 
      setActionBarBackground(); 
     } 
    }; 

    private void setActionBarBackground() { 
     ActionBar ab = getSupportActionBar(); 
     //Drawable d = WidgetUtils.getActionBarDrawable(TestActivity.this, 0xFF00FFFF); 

     GradientDrawable gd = new GradientDrawable(
       GradientDrawable.Orientation.TOP_BOTTOM, 
       new int[]{0xFFFFFFFF, 0xFF000000}); 
     gd.setCornerRadius(0f); 
     ab.setBackgroundDrawable(gd); 
    } 

} 

test_activity.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="button"/> 
    <EditText 
     android:id="@+id/ET_test" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:singleLine="true" 
     android:maxLines="1" 
     android:lines="1" 
     android:inputType="number" 
     android:imeOptions="actionDone" 
     android:nextFocusUp="@id/ET_test" 
     android:nextFocusLeft="@id/ET_test"/> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="button2"/> 

</LinearLayout> 

回答

18

亨德里克的解決方法是卓有成效的,但不使用自定義標題視圖時。這可以代替:

actionBar.setBackgroundDrawable(newBackground); 
actionBar.setDisplayShowTitleEnabled(true); 
actionBar.setDisplayShowTitleEnabled(false); 

雖然您可能需要一個標題集才能工作。

+1

此操作無需標題。 –

+0

感謝@Mat爲我工作,但如果你需要的標題你可以改變使標題:) – Antarix

+1

的狀態,如果你不知道,如果標題顯示或不工作的:\t \t \t 布爾isDisplayingTitle =( actionBar.getDisplayOptions()&ActionBar.DISPLAY_SHOW_TITLE)!= 0; actionBar.setDisplayShowTitleEnabled(!isDisplayingTitle); actionBar.setDisplayShowTitleEnabled(isDisplayingTitle); –

0

嗯,這似乎是一個瘋狂的邊緣情況,並與這兩個ActionBarSherlock和Android發生ActionBar的實現。我結束了調用ab.hide()和ab.show()解決它... :(

+0

This Works。一個問題。你有沒有想過如何防止屏幕其餘部分的上升和下降? – javahead76

0

我使用ActionBarSherlock以及我也想要通過調用更改ActionBar的背景Drawable但是我發現在背景顏色設置一次之後,對setBackgroundDrawable()的後續調用似乎沒有任何效果,我認爲這與您遇到的問題類似

若要將任意時間設置爲setBackgroundDrawable()繞過它,我欺騙了,我只是在我的佈局中放置了一個View,它會出現在ActionBar的後面,並且讓ActionBar本身透明,View的高度很容易設置爲正確的高度,通過調用getHeight()他ActionBar。

4

我遇到了同樣的問題。當我動態地將setBackgroundDrawable()的動作欄背景更改爲新的GradientDrawable時,動作欄全部變黑。

我發現這是連接到操作欄的標題。我不使用標題並將其設置爲空字符串。出於測試目的,我將它設置爲測試字符串「測試」,而setBackgroundDrawable()的問題立即消失。看起來像ICS中的一個bug。

因此,而不是隱藏和顯示操作欄下面的替代方法適用於我:

actionBar.setBackgroundDrawable(newBackground); 
actionBar.setTitle("."); 
actionBar.setTitle(""); 
+0

此變通辦法對我無效:(只有隱藏/顯示作品可能會在此情況下有效 – fvisticot