2012-06-12 4 views
11

ActionBarのバックグラウンドをハンドラから変更しようとしています。最終的な目標は、ハンドラをAsyncTaskに渡すことですが、今度はHandler.sendMessage()をThreadから呼び出しても異常な動作が発生します。デバッガを介して、HandlerがMessageを受け取り、続いてsetActionBarBackground()を最後まで実行することがわかります。ActionBar setBackgroundDrawable()スレッド/ハンドラから背景をnullにする

青いボトムストロークのデフォルトの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の実装

+0

これは機能します。一つの質問。スクリーンの残りの部分の浮き上がりを防ぐ方法を見つけましたか? – javahead76

0

私はActionBarSherlockも使用していますが、私もActionBarの背景をコールで変更したいと思っていました(Drawable)。しかし、私は、背景色を1回設定した後に、setBackgroundDrawable()の呼び出しは何の効果もないようですが、これはあなたが遭遇した問題と似ていると思います。

To私はちょうどViewを配置し、ActionBarの背後に表示され、ActionBar自体を透明にしました。Viewの高さは、tでgetHeight()を呼び出すことによって簡単に正しい高さに設定されます。彼はActionBar。

4

同じ問題が発生しました。 setBackgroundDrawable()のアクションバーの背景を新しいGradientDrawableに動的に変更すると、アクションバーはすべて黒に変わります。

これはアクションバーのタイトルに関連付けられていることがわかりました。私はタイトルを使用せず、空の文字列に設定しました。テスト目的のために、私は代わりにテスト文字列 "Test"に設定し、setBackgroundDrawable()の問題はすぐに消えました。 ICSのバグのように見えます。だからではなく、隠れて、次の回避策は、私の作品アクションバーを示すの

:カスタムタイトルのビューを使用した場合

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

この回避策は私にとってはうまくいかない:(... hide/showのみで動作する場合があります – fvisticot

関連する問題